The origin is rooted in the Bhagavata Purana and Vishnu Purana accounts of Krishna's birth and childhood, and the story of Kamsa.
Lord Krishna was born at midnight in a prison in Mathura to Devaki and Vasudeva. His uncle, King Kamsa, had been warned that Devaki's eighth child would destroy him, so he imprisoned the couple. At the divine birth, the prison doors opened and Vasudeva carried the baby Krishna across the Yamuna to Gokul.
Bhagavata Purana; Vishnu Purana