The legend of Divodasa was originally mentioned in Shiva Purana and/or Linga Purana. The legend has several variations with few differences.
It was the time when the world suffered. Humanity was falling in the reign of years long famine. Order of civilization and dharma were disappearing. Then, Lord Brahma, the creator of universe, was concerned. After a long search for a solution, he found a sage of royal blood performing hard tapa in the jungle near the city of Banaras. Lord Brahma made his decision.
Ripunjay was deep submerged in tapa when he heard a voice inside his head, “Open your eyes, my child.”
Ripunjay opened his eye to find the four headed God emerging from his fire. Devoted prince bowed to Lord Brahma.
Brahma looked at Ripunjay kindly and said, “O man of high soul! Time has come when you should bear the crown. Take the world under your throne and save the humanity from fall. By my aid, thou shalt reign over the world and reform the dharma.
Ripunjay listened calmly and fathomed the offer made by the Lord. He made some decisions of his own and then sopke, “My lord, I shall happily do what you command. But on one condition. I wish to rule in peace, so I could reform the dharma without any disturbance. I wish all Gods and Godly entities to not set feet upon my land and remain in the heaven only.”
“So be it” said Brahma.
Therefore, on the command of Brahma, all Gods left to the heaven. Ripunjay was then renamed to Divodasa. He took the throne at the most divine city of the world– Kashi.
Divodasa worked vigorously to bring order in the decaying world. With the favours obtained from Lord Brahma, he established a rule so flawless that was never seen before. Under his reign, his subjects prospered and justice flourished.
The king grew stronger and stronger. So strong that Gods began to become jealous. Indra– the God of rain, Agni– the God of fire, and Vayu– the God of air took rain, fire, and air back in order to disrupt the rule of the king. But Divodasa proved to be stronger than his rivals had estimated. He created rain, fire, and air of his own; for such mighty divine powers he had obtained from his flawless rule.
He continued to rule for eight thousands years. Gods were spiteful now. They wished to get rid of the earthly king. But they had no answer to powers of Divodasa.
Lord Shiva, one of the supreme trinity, was very desirous to visit his favourite city Kashi. But due to Brahma’s boon to Divodasa, he was unable to set feet in Kashi. Struck with grief he decided to cast some dents into the flawlessness of Divodasa’s rule in order to reduce his powers and to end his reign.
He summoned sixty yoginis on the advice of his wife Goddess Parvati. He commanded the band to go to Kashi and disrupt the perfectness of Divodasa’s rule. When the yoginis arrived at Kashi, they were overwhelmed after seeing the perfectness. They abandoned the thought of causing any harm to it. Instead, they decided to never leave the city. When they did not return, Lord Shiva sent Surya– the God of Sun, to Kashi. But once he saw the city, Surya walked on the same path as the band of Yoginis.
Lord Shiva was devastated when Surya did not return either. His eagerness to return to his beloved Kashi had taken away peace of his mind. He then called upon Lord Brahma. He persuaded him to undo what he had done; for aim of Divodasa’s reign was fulfilled and world was in order.
Lord Brahma admitted that even he could not have removed Divodasa from the throne. But he assured Shiva to do something. He took the guise of a Sadhu– a holy man and entered Kashi. Soon he had the king under his influence. And then he recommended Divodasa to arrange an Ashwamedh Yagya with ten horses. An Ashwamedh Yagya is a ritual practised by mighty kings, in which a horse is made to roam freely through all lands. Army of the yagya practising king follows the horse. The land through which the horse walks through, comes into the supremacy of the yagya performing king. If someone refuses to accept the supremacy of the king, then they have to battle against the army following the horse.
Brahma suggested the mighty king to practice the yagya with ten horses, in order to make it more difficult. In this way Divodasa’s army was divided into ten parts to follow each horse. Ten armies of the king followed the ten horses in ten directions. But yet no enemy could capture any of the ten horses. To Brahma’s dismay, horses came back safely with their armies. Lord Brahma was much ashamed of the failure of his trickery and abandoned the thought of any other attempt.
Lord Shiva saw few more attempts going in vain; and his desire to return to his beloved city was ever increasing. At last, he approached Lord Vishnu, who is also one of the supreme trinity. Vishnu is the one who often rescues the Gods in such crisis. A strong scheme was laid out by him, again.
In accordance to the plan, Ganesha, son of Shiva arrived at Kashi in the disguise of an astrologer. Having a long term scheme, he established his residence near the city and began serving the citizens as astrologer. Fame of the astrologer spread in the kingdom. Touched by wind of the fame, the queen also sought his service, and was impressed by the astrologer. Once the queen was impressed, astrologer did not need very long time to reach the royal court of the king.
In the royal court, he pretended to study the palm of king attentively. At the end, he declared that a holy and wise sadhu would come to see the king on the eighteenth day. Council of the wise man would bring wealth of enlightenment. Divodasa did not pay much attention to this prophecy; but on the eighteenth day, a Buddhist monk with a divine light about him, indeed came to his door. The monk was, in fact, Lord Vishnu in disguise. Divodasa had to be tricked by the of one of the supreme lords of the universe, if lord wished so, for he was only a man. The monk rained words of wisdom upon him. Which were, in truth, little drops of tricks of lord Vishnu.
Vishnu created an illusion by words. Divodasa was deceived and he began to faults in his flawless rule. He saw dissatisfaction and unhappiness among his subjects. He was distressed. He asked for the path to deliverance. Vishnu told him to give up his kinghood and devote himself to the search of peace. The deceived king obeyed the holy man. He made his son the new king and devoted himself to God Shiva to ease his sins. Thus, the mightiest of all men was at last retired, but only by trickery and conspiracy of Gods.
Delighted lord Shiva returned to his beloved Kashi and swore to never leave the city.
In the more ancient versions, Vishnu was described to have taken the guise of a sadhu, not a Buddhist monk. Many scholars believe that Buddhist bits were added to the legend when Buddhism had taken over the large part of India and Brahmins were not pleased to see the kings paying reverence to Buddhism.