The origin is rooted in the Devi Mahatmya (part of the Markandeya Purana), describing the battle with Mahishasura, and in ancient harvest and seasonal worship of the Divine Mother.
According to the Devi Mahatmya, the goddess fought the buffalo-demon Mahishasura for nine days and nine nights, and on the tenth day, Vijayadashami, she defeated him. The nine nights honour the three goddesses Durga, Lakshmi and Saraswati.
Devi Mahatmya / Markandeya Purana