Śrāddhadeva, or Vaivasvata Manu, the father of Mahārāja Ikṣvāku, was sonless before Ikṣvāku’s birth, but by the grace of the great sage Vasiṣṭha, he performed a yajña to satisfy Mitra and Varuṇa.
Then, although Vaivasvata Manu wanted a son, by the desire of his wife he got a daughter named Ilā. Manu, however, was not satisfied with the daughter.
Consequently, for Manu’s satisfaction, the great sage Vasiṣṭha prayed for Ilā to be transformed into a boy, and his prayer was fulfilled by the Supreme Personality of Godhead.
Thus Ilā became a beautiful young man named Sudyumna.
That hero Sudyumna, accompanied by a few ministers and associates and riding on a horse brought from Sindhupradeśa, once went into the forest to hunt.
He wore armour and was decorated with bows and arrows. While following the animals and killing them, he reached the northern part of the forest.
There in the north, at the bottom of Mount Meru, is a forest known as Sukumāra where Lord Śiva always enjoys with Umā. Sudyumna entered that forest.
When he entered the forest, he saw himself transformed into a female and his horse transformed into a mare. His followers also saw their identities transformed and their sex reversed, they were all very morose and just looked at one another.
Great saintly persons who strictly observed the spiritual rules and regulations and whose own effulgence dissipated all the darkness of all directions once came to see Lord Śiva in that forest.
When the goddess Ambikā saw the great saintly persons, she was very much ashamed because at that time she was naked. She immediately got up from the lap of her husband and tried to cover her breast.
Seeing Lord Śiva and Pārvatī engaged in sexual affairs, all the great saintly persons immediately desisted from going further and departed for the āśrama of Nara-Nārāyaṇa.
Thereupon, just to please his wife, Lord Śiva said, “Any male entering this place shall immediately become a female!”
Since that time, no male had entered that forest. But now King Sudyumna, having been transformed into a female, began to walk with his associates from one forest to another.
Sudyumna had been transformed into the best of beautiful women who excite sexual desire and was surrounded by other women.
Upon seeing this beautiful woman loitering near his āśrama, Budha, the son of the moon, immediately desired to enjoy her.
The beautiful woman also desired to accept Budha, the son of the king of the moon, as her husband. Thus Budha begot in her womb a son named Purūravā.
Upon seeing Sudyumna’s deplorable condition, Vasiṣṭha was very much aggrieved. Desiring for Sudyumna to regain his maleness, Vasiṣṭha again began to worship Lord Śaṅkara [Śiva].
Therefore, to satisfy him and to keep his own word to Pārvatī, Lord Śiva said to that saintly person, “Your disciple Sudyumna may remain a male for one month and a female for the next. In this way, he may rule the world as he likes.”
Thus being favoured by the spiritual master, according to the words of Lord Śiva, Sudyumna regained his desired maleness every alternate month and in this way ruled the kingdom, although the citizens were not satisfied with this.
Sudyumna had three very pious sons, named Utkala, Gaya and Vimala, who became the kings of the Dakṣiṇā-patha.
Thereafter, when the time was ripe, when Sudyumna, the king of the world, was sufficiently old, he delivered the entire kingdom to his son Purūravā and entered the forest.
Reference: Śrīmad-Bhāgavatam (Bhāgavata Purāṇa) » Skandha 9: Liberation