From Ganesh puran,
Once Asrura, Narantak & Devantak, started torturing people of Kashi. On Kashiraj's request Shivji-Paravti had to leave too. Shivji felt bad about it. His son Ganesh came to help him. Kashiraj prayed to Ganeshji to, help hifight the Asuras.
After discussing with the Ganas, who were ready to fight under his leadership, Ganeshji told them, Narantak is powerful, his army is much bigger 7 stronger then us, but we need not worry, we are fighting for the people, go & inform each house & call them to fight.
Ganas did the same. Narantak's army against Ganas with people. But narantak proved to be stronger after days of fighting. He killed many Ganas. But Ganeshji did Shankhnaad, called their remaining warriors back, told him if you'll run away, this Asur will kill you, it's better fight with them once again, we will win..
This time Narantak came to fight with Ganesh, who removes a bana, given to him at the time of his Yagyopavit by Shivji, who had told him, this bana will save you. Ganesh remembered mother Parvati, Suryadev & killed Narantak with it.
Everyone was happy, kashiraj prayed to Ganesh, told himto be their Senapati. Ganeshji refused saying his work is over, now he will go back.
Ganeshji along with some Ganas left Kashi, everyone was quitesad. To remember Ganesh, hios idol was made & kept in a temple. It was Bhadrapad Chaturthi day.
On his way, Ganesh passed many states, was worshipped everywhere, he went, with his idol being made & worshipped in temple. They celebrated it like a festival. Like this 10 days journey was over.
Now what? was thequestion. Ganeshji decided to leave his body, since his work was over& there was peace among people.
Ganas got scared. Ganeshji told them not to worry, he went to the river, through yoga, left his body, that was the evening of Ananta Chaturdashi.
To rember this event, these 10 dayswere decided for Ganesh's sthapana,pooja etc. That's how Ganeshotsav started.