There used to live a brahmin at Avantikapuri. He was a great devotee of lord Shiva and used to worship daily.
This brahmin had four sons whose names were Devapriya, Medhapriya, Sukrit and Dharmabahu. By the blessings of lord Shiva the brahmin enjoyed a very happy and contented life. After his death, the brahmin’s son continued the tradition of Shiva’s worship.
There was a mountain named Ratanak on which a demon named Dushan used to live. Being intoxicated by the boons received from Lord Brahma, he used to torment all the people. He had all the surrounding area under his control except the house in which the brahmin family lived.
Dushan ordered his fellow demons to bring the brahmin after capturing them. All the demons went to Avantikapuri and created havoc. They went to the brahmins and told them about Dushan’s order. The brahmins were busy doing worship at that time so they did not give ear to
their words. The brahmins continued with their worship. The demon became very angry and tried to attack them.
Suddenly the earth cracked with a tremendous sound and lord Shiva manifested from within the cracks. He killed all the demons. He then went to the Ratnamala mountain and killed Dushan. He again returned to the brahmins and expressed his desire of fulfilling their wishes. The brahmins expressed their desire of getting liberated from the bondage’s of this world. They also requested him to remain at that place. Lord Shiva accepted their request and established himself in the form of Mahakal.