The mythological basis of the celebration is found in the Puranas. Parvati, while taking a bath, wanted somebody to guard from outside. She got the idea of making an idol out of the dirt that came from her body. She placed the idol outside. The idol did not even let her husband enter. Shankar became furious and instantly severed the head of the idol.
Parvati finished her bath and came out. She explained to her husband the reason for placing the idol before the door. The idol was her creation. In fact the idol was her son ("manas putra"), and consequently also Shankar's son.
Shankar felt sorry for what he had done. His servant Gan was standing nearby. Shankar ordered him to go and bring the head of the first living being he would meet. The servant saw an elephant, and he at once cut his head and took it to Shankar. Shankar joined the elephant's head to the body of Parvati's son. "Gaj" means elephant and "anan" means head, so Parvati's son became known as "Gajanan". Shankar made him the deity of his armies and thus his name became Ganesh.