The story of Lord Ganesha and the demon Matsara is mentioned in the Matsara Purana, one of the eighteen Mahapuranas of Hinduism.
According to the legend, Matsara was a powerful demon who had become arrogant and had started tormenting the gods and sages. He was so powerful that no one could defeat him, and he started thinking of himself as invincible. One day, he decided to attack Lord Brahma, the creator of the universe.
Lord Brahma, being one of the Trimurti, was powerful but he was not capable of defeating Matsara alone. So, Brahma along with other gods decided to seek help from Lord Shiva and Lord Vishnu. The three deities then decided to create a powerful being who could defeat Matsara.
With their combined power, they created Lord Ganesha, who was endowed with extraordinary strength, wisdom, and intelligence. Lord Ganesha was a perfect warrior, with the head of an elephant, a human body, and a snake around his neck.
When Matsara came to attack Brahma, he was met with Lord Ganesha, who stood in his way. A fierce battle ensued between the two, with Matsara using all his powers and weapons against Lord Ganesha.
But Lord Ganesha, being wise and intelligent, used his wit to defeat Matsara. He swallowed Matsara whole, trapping him in his stomach. Matsara was so powerful that Lord Ganesha couldn’t digest him, and so he started to cause pain and discomfort to Lord Ganesha.
Seeing Lord Ganesha in distress, Lord Shiva and Lord Vishnu appeared before him and asked him to release Matsara. Lord Ganesha then released Matsara, who realized the greatness of Lord Ganesha and begged for his forgiveness.
Lord Ganesha forgave Matsara and asked him to leave the path of evil. From that day onwards, Matsara became a devotee of Lord Ganesha and spent the rest of his life in meditation and worship of Lord Ganesha.
This story shows Lord Ganesha’s incredible strength, intelligence, and wisdom, and how he was able to defeat even the most powerful demons with his wit and courage.