"When a dominant male is lost from a social group, the largest female transforms into a fertile male in 10 days flat," said co-lead author Dr Erica Todd, of Otago's Department of Anatomy.
Females began this transformation within minutes, first changing colour and displaying male-like behaviours.
Their ovaries then start to regress and fully functional testes grow in their place.
"How this stunning transformation works at a genetic level has long been an enigma."
Using the latest genetic approaches, high-throughput RNA-sequencing and epigenetic analyses, the researchers discovered when and how specific genes are turned off and on in the brain and gonad so that sex change can occur.
"Our study reveals that sex change involves a complete genetic rewiring of the gonad. We find that genes needed to maintain the ovary are first turned off, and then a new genetic pathway is steadily turned on to promote testis formation."
This chain reaction began when a gene called aromatase, which was responsible for making the female hormone estrogen, was turned off.
What triggered aromatase to turn off was still unknown, but the stress of social change resulting from the loss of the existing dominant male might be an important signal in turning off the genetic pathway that maintains the ovary.
Co-lead author PhD candidate Oscar Ortega-Recalde said the amazing transformation also appeard possible through changes in cellular "memory".
"In fish and other vertebrates, including humans, cells use chemical markers on DNA that control gene expression and remember their specific function in the body," he said.
"Our study is important because it shows that sex change involves profound changes in these chemical marks, for example at the aromatase gene, thus reprogramming cell memory in the gonad towards a male fate."
As many genes important for sexual development in fish are also important in other animals, the team's discovery has practical applications for humans.
"Understanding how fish can change sex may tell us more about how complex networks of genes interact to determine and maintain sex, not only in fish but in vertebrate animals generally," Todd said.
The "remarkable" speed at which a bluehead wrasse's ovaries can be transformed into new testes also opens up the possibility of applications in tissue and organ engineering, with potential benefits to medical science.
Furthermore, understanding sex change at a genetic level may also have benefits for aquaculture industries, as about 500 fish species, including many of commercial value such as New Zealand's iconic blue cod, can also change sex.
Co-author Professor Jenny Graves, of Melbourne's La Trobe University, said the project also linked to studies of sex reversal in Australian dragon lizards.
"With dragon lizards the trigger for sex change is temperature, which overrides genes on the male sex chromosomes and causes embryos to develop as females," Graves said.
"Sex reversal in dragons and the wrasse involve some of the same genes, so I think we are looking at an ancient system for environmental control of gene activity."