Snakes or Nagas are constant companions of Muruga – who is also known as Subramanian, Kartikeya and Skanda. The reason why nagas are associated with Muruga is a story of repentance. Legend has it that once Muruga asked Brahma the meaning of Pranava (OM). Brahma was not able to provide a satisfactory answer. An angry Muruga imprisoned Brahma.
Later Shiva intervened and the matter was settled.
Muruga then felt that he did not act properly by imprisoning Brahma. For the sin committed, he took the form of a huge snake and started living in this form.
Goddess Parvati was unhappy with the snake form of Muruga. She complained to Shiva.
Shiva then asked Goddess Parvati to observe 108 Sashti Vratam to get back her son.
When the 108 Sashtis were completed, all the gods arrived to witness the meeting of Muruga and Goddess Parvati.
Muruga gave away the serpent form and appeared in his original form.
There is a slight difference regarding the final part of the story in some scriptures. It is Vishnu that advices Goddess Parvati to observe the Vrata. On the 108 Sashti day Muruga appears as a huge snake. He gets back the original form when Vishnu touches the snake.