They report in the journal Science that lowering the activity of just two genetic pathways produces the change.
Evolution from a species consisting of males and females into one consisting of only males and hermaphrodites happens naturally in many nematodes.
A team of US researchers says their experiment explains how this might take place.
They say it also provides a simple model helping scientists to work out the mechanism of evolutionary change.
The researchers chose to study the evolution of female worms into hermaphrodites because it was a “striking change” that occurred relatively recently.
Ronald Ellis, a biologist from the University of Medicine and Dentistry New Jersey in the US, who led the research, said that most big evolutionary changes within species happened too long ago to study at the genetic level.
“But this dramatic change happened fairly recently and in a group of animals that we know a lot about… that's why we're studying it to find out how complex traits are created,” he told BBC News.