Puranas contain many stories and legends describing the origin of this festival.
According to one, during the samudra manthan, a pot of poison emerged from the ocean. This terrified the Gods and demons as the poison was capable of destroying the entire world, and they ran to Shiva for help. To protect the world from its evil effects, Shiva drank the deathly poison but held it in his throat instead of swallowing it. This made his throat turn blue, and he was given the name Neelakantha, the blue-throated one. Shivaratri is the celebration of this event by which Shiva saved the world.
According to another legend in the Shiva Purana, once the other two of the triads of Hindu Gods, Brahma and Vishnu, were fighting over who was the superior of the two. Horrified at the intensity of the battle, the other gods asked Shiva to intervene. To make them realize the futility of their fight, Shiva assumed the form of a huge column of fire in between Brahma and Vishnu. Awestruck by its magnitude, they decided to find one end each to establish supremacy over the other. Brahma assumed the form of a swan and went upwards and Vishnu as Varaha went into the earth.
But light has no limit and though they searched for thousands of miles, neither could find the end. On his journey upwards, Brahma came across a Ketaki flower wafting down slowly. When asked where she had come from, the Ketaki replied that she had been placed at the top of the fiery column as an offering. Unable to find the uppermost limit, Brahma decided to end his search and take the flower as a witness.
At this, the angry Shiva revealed his true form. He punished Brahma for telling a lie, and cursed him that no one would ever pray to him. The Ketaki flower too was banned from being used as an offering for any worship, as she had testified falsely. Since it was on the 14th day in the dark half of the month of Phalguna that Shiva first manifested himself in the form of a Linga, the day is especially auspicious and is celebrated as Mahashivaratri. Worshipping Shiva on this day is believed to bestow one with happiness and prosperity.
A legend explains the all-night worship of Shiva on Shivratri. There was once a poor tribal man who was great devotee of Shiva. One day he went deep into the forest to collect firewood. However he lost his way and could not return home before nightfall. As darkness fell, he heard the growls of wild animals. Terrified, he climbed onto the nearest tree for shelter till day-break. Perched amongst the branches, he was afraid he would doze and fall off the tree. To stay awake, he decided to pluck a leaf at a time from the tree and drop it, while chanting the name of Shiva.
At dawn, he realized that he had dropped a thousand leaves onto a Linga to keep himself awake, the tribal plucked one leaf at a time from the tree and dropped it below which he had not seen in the dark. The tree happened to be a wood apple or bel tree. This unwitting all-night worship pleased Shiva, by whose grace the tribal was rewarded with divine bliss. This story is also recited on Mahashivaratri by devotees on fast. After observing the all-night fast, devotees eat the Prasad offered to Shiva.