Ganesha’s first avatara is of Vakratunda (twisted trunk) an embodiment of the Absolute Impersonal aspect of Godhood called Brahman in the form of Ganesha.
A demon named Matsara was born out of the Pramaada (Heedlessness) of Indra, the king of Gods.
This demon performed severe penance and invoked the grace of Lord Shiva and obtained the boon of fearlessness from Him.
Having obtained the boon he went about conquering all the three worlds,” He was coronated as the king of Asuras by their preceptor Sukracharya. The defeated gods went to Kailasa and prayed to Lord Shiva for protection. On hearing the mission of gods, Matsara went to Kailas and vanquished Lord Shiva also.
When the gods were at their wit’s end to know what to do next. Lord Dattatreya came there. He advised the gods to invoke the grace of Lord Vakratunda and imparted to them the secret of the monosyllable mantra Gam. All the gods including Shiva did penance accordingly and at last Lord, Vakratunda appeared and assured them that he would subdue the demon. The demon was so terrified at the sight of Vakratunda that he surrendered to Him and sought refuge at His feet. The Lord forgave him and restored the lost glory and kingdoms to the gods, and to the various kings on earth.