Quite a few fish species do this actually, most notably Clown fish.
All start off as male or gender neutral, the largest becomes female, the second largest in the community becomes the male. The rest wait their turn. If the female dies, the male becomes the female, and the next male or gender neutral becomes the male.