In clownfish, all individuals are born male; the dominant fish in the group becomes female. If the female disappears, the breeding male changes sex within a few weeks and takes her place. This system ensures that a breeding pair always exists in the anemone.
A strict hierarchy within the anemone
A group of clownfish lives in the same anemone: a female — the largest individual —, a breeding male, and a few subordinates who do not reproduce. Rank is measured by size, and everyone's growth is literally restricted by the presence of those dominant.
When the female dies, the cascade is triggered: the breeding male reverses its sex — a phenomenon called protandrous hermaphroditism — and the largest subordinate becomes the new breeding male. Leaving the anemone to look for a partner would be suicidal on a reef: changing sex on the spot is the safest solution.
A widespread phenomenon among fish
Sexual inversion is not an isolated curiosity: many reef fish are capable of it, often in the other direction - from female to male -, like wrasses or groupers.