The story of Naga Panchami is described in Chapter 24 of Varaha Purana.
The Progeny of Ananta, Vasuki, Kambala, Karkotaka, Padma, Maha Padma, Sanka and Kulika filled the earth and they were crooked, heinous and violent and with their fangs they stung people and reduced them to ashes. This used to reduce human population and virtuous men went to abode of Lord Brahma and asked him for a solution. Then Brahma cursed the serpents as follows.
Brahma said to Serpents,
- Since you are annihilating my progeny, the men, in another age, in the Swayambhu Manvantara, by curse of the mother, you will face a
serious decline.
Serpents said,
- O God. We were made crooked by nature by you. Our Venom, cruelty, and the use of eye as weapon were all given by you. The intensity of
all that was given may be reduced.
- O God. Please allot us separate regions.
Brahma said,
- Patala, Vitala, Sutala are the regions allotted to you. You may all have your abode there.
- I ordain that you shall enjoy all pleasures till seventh Manvantara.
At the beginning of Vaivasta Manvantara you will become relations of Devas as well as of Garuda.
Then all your progeny will be consumed by fire but for your own selves there will be no danger
The end of such serpents as are fierce and insolent will certainly occur, but not of others. You may eat and bite person if he is
destined, but also to retaliate when somebody injures you.
The serpents thereafter went to nether world allotted to them. All this happened on Panchama tithi. So, this tithi is auspicious and capable of removing all sins.
If one remains austere on that day, give us all that is sour in food and bathes the images of Nagas in milk, they become friendly towards him.