Maharishi Vyas created Mahabharata in poetry form. He was not able to write it in prose form.
Hence he worshipped Lord Ganesh and requested him to write Mahabharata for him. Lord Ganesh agreed to do so.
The process of writing was going to take days and nights, this would have tired Lord Ganesh plus Lord was not to take even water during this writing. This would have raised the temperature of the Lord’s body. Hence Maharishi Vyas applied paste of mud on Lord Ganesh’s body.
On Bhadrapad Shukla Chaturthi the writing started and ended on Anant Chaturthi. At the end of the writing on Anant Chaturdashi, Maharishi Vyas observed that Lord Ganesh’s body temperature had risen a lot, hence he immersed the Lord in water to cool him down and wash off the mud.
All through the ten days Maharishi Vyas offered Lord Ganesh best of foods (naivaidhya) and prayed . Hence started the practice of Ganesh festival, bringing Lord Ganesh home, making him sit, offering him the best of foods and on the tenth day immersing the idol.