The legend of Lord Ganesha and King Divodasa is mentioned in the Shiva Purana. According to the legend King Divodasa was the religious and pious ruler of Kasi (Kashi). Everyone was happy in the kingdom and the people prospered under his rule. The ruler made an agreement with Lord Brahma that as long as he served as the ruler over the region, all the gods and other heavenly beings should stay away from the kingdom of Kasi. Moreover the deities should not create any disruption in the kingdom. Lord Brahma agreed to his stipulation but with a condition of His own. Brahma stated that Divodasa should prove to be a brilliant ruler and all the residents of Kasi and people visiting the kingdom should be treated well. The king agreed to the condition of the Lord and administered Kasi with excellence.
However, Lord Shiva became upset as He had to stay away from Kasi. So He wanted to make Divodasa commit some mistake. Shiva sent 64 Yoginis to create hindrances, but the yoginis became captivated by the tranquil and beautiful surroundings of the kingdom of Kasi. They eventually decided to reside in Kasi. After the yoginis failed to complete their task, Lord Shiva sent Lord Surya to the Kingdom and instructed him to create obstruction in Kasi and to defeat Divodasa in his determination. Lord Surya took the disguise of a Brahmin, a businessman, a religious propagator, a learned scholar, an astrologer and others. But he failed to discover any flaw with the administration of King Divodasa or any person in the kingdom. Lord Surya also became mesmerized with the beauty of Kasi. The Lord knew if he returned to Shiva fulfilling His wishes, Lord Shiva would be upset. Thus Surya also decided to reside in Kasi.
Later, Lord Brahma was sent to Kasi by Lord Shiva with the same objective. Brahma disguised himself as an old Brahmin and went to visit King Divodasa. The pious ruler welcomed him with honour. The old Brahmin announced that he wanted to conduct the Aswa Medha Yagna and asked the ruler to make arrangements and provide all the materials. Thus Lord Brahma, with the assistance of Divodasa, performed ten Aswa Medha Yagnas. Shiva then sent his celestial army, known as Ganas to the kingdom of Kasi. The Ganas also became enthralled by the magnificence of region and decided to reside in Kasi. They also installed several Shiva Lingas (Lingam) at various places throughout the kingdom.
Ultimately Lord Shiva called Lord Ganesha, his son and requested him to create disturbances in the administration of King Divodasa. Ganesha took the disguise of an old astrologer when he reached Kasi. He influenced the dreams of the residents and later told them the ill effects of those dreams. The Lord interpreted the positions of various planets in the horoscopes of the people of Kasi. Eventually the elephant headed deity gained entry into the palace of Divodasa. As time passed Lord Ganesha managed to impress the Queen who told the King that he respect and adore the old astrologer.
Later the King and the Queen invited the old astrologer (Ganesha) into the royal court. Divodasa humbly welcomed Lord Ganesha and He blessed the King. The King asked the old astrologer to predict the future condition of the kingdom. The old astrologer told several things about Divodasa and Kasi. Ganesha in the form of the astrologer as said that from that day, on the eighteenth day a Brahmin will come to the King and will give him some valuable advice which the King needed follow. Later the old astrologer left the palace.
Ultimately Lord Vishnu went to the kingdom of Kasi. He took the guise of a as a Brahmin and approached King Divodasa on the eighteenth day. The ruler welcomed the Brahmin with honour. The Brahmin (Lord Vishnu) preached all good things to Divodasa. The King mentioned that the residents of Kasi were very happy under his governance and there was a sense of religious passion all over. The Brahmin then advised King Divodasa to install a Shiva Ling in the kingdom. He further told that a divine aircraft would come down to Kasi from heaven on the seventh day and it will take King Divodasa to the abode of Lord Shiva.
Divodasa became very happy with this prediction. He installed a Shiva Linga in the kingdom which was later known as Divodaseshwer. Moreover the King gave Kasi to Rajkumar Samaranjayan and started praising and praying to Lord Shiva.