The story of Shiva and Gajasura (elephant demon) involves an interesting version of the origin of lord Ganesha and his elephant head. The story portrays the fierce aspect of Shiva as he slays Gajasura and attaches his head to his son’s neck.
Gajasura was a demon with all the characteristics of an elephant. Once he was undergoing severe penitence to please Shiva. Delighted with this devotion, Shiva appeared before him and asked for his wish. Gajasura expressed his desire to be able to continuously release fire from his body so that no one could ever dare to approach him. Shiva granted his wish. Gajasura continued his penitence and Shiva appeared before him once again. Demon expressed his wish that Shiva should dwell in his stomach. Shiva instantly agreed and went inside Gajasura’s belly.
Shiva’s wife Parvati was searching for him everywhere and got worried when unable to find him. She approached Vishnu and sought his help in finding Shiva. Vishnu realized that Shiva is inside the asura’s stomach and went on a mission to release him.
He took the form of a flutist and escorted Nandi (the bull of Shiva) to Gajasura. Vishnu made Nandi into a dancing bull. It presented a bewitching dancing performance before the asura. The asura got over-delighted and told the flutist to ask for a wish. Vishnu asked asura to liberate Shiva from his stomach. Gajasura suddenly realized that Vishnu is standing before him in the disguise of a flutist. He threw himself at his feet and asked for pardon. He liberated Shiva and asked him for one last gift. He asked that everyone should remember him adoring his head when he is dead. Shiva then brought his own son there and substituted his head with that of Gajasura. From this time onwards, any new action or venture will begin with the adoration of Ganesha.