The mechanisms that determine sex and gender in humans are quite complicated and there are still many things that are not yet fully understood. In certain fish things are even more complex, and are remarkably fluid. There are several species of fish that seriously challenge the idea of fixed gender and sex. Cirrhilabrus finifenmaa for example, a species of rainbow-colored reef fish from the Maldives, spend their lives as sequential hermaphrodites. A sex change is a normal part of their life cycle; they start their lives as females, and change later to be males. This phenomenon is not unusual in wrasses like Cirrhilabrus as well as many others, it is known to occur in at least 27 families, following three different patterns: sex change can either be protogynous (female-to-male), protandrous (male-to-female), or bidirectional. These transformations require radical changes, not only regarding sexual organs but also morphology and behavior. The determination as well as the expression of sex is incredibly diverse in teleost fish. Genetics, environmental conditions like temperature, pH balance, and population density and interaction are some of the factors that contribute to the development of their sex.
Sources: Todd, E. V. et al., “Bending Genders: The Biology of Natural Sex Change in Fish”, Sexual Development, vol.10, (2016): 223-241.