The god who came out of the ground
Avanti — the city later called Ujjayini — is home to a brahmin named Vedapriya. He studies the Vedas, keeps the sacrificial fires, performs the Vedic rites and worships an image of Shiva every day. He has four sons, Devapriya, Priyamedhas, Sukrita and Dharmavahin, and all four are devoted to Shiva as he is. The Purana credits their merit with the prosperity of the city.
On a hill called Ratnamala lives Dushana, a king of the Daityas. Boons from Brahma have made him contemptuous of the whole universe. He drives out the gods and sets about ending Vedic and Smarta rites across the earth, halting worship at shrine after shrine.
Dushana marches on Avanti with a large army. He sends four Daityas ahead with an order: the brahmins are to give up the worship of Shiva and the study of the Vedas, or be killed. The Daityas surround the city. The townspeople come to the brahmins in fear and ask what is to be done. The brahmins answer that they have no army and no weapons, and that apart from Shiva they have no refuge.
They make an earthen linga and sit before it in worship. Dushana orders them to be killed or bound, but they are absorbed in meditation and do not hear him. Then the ground beside the earthen linga caves in with a loud report, and Shiva rises out of the chasm in a fearsome form. He names himself Mahakala — great Kala, death to the wicked. With a single sound, "Hum", he burns Dushana and his army to ashes, and the soldiers who survive are routed. The gods, led by Vishnu and Brahma, arrive.
Shiva offers the brahmins a boon. They ask for two things: release from the cycle of worldly existence, and that Shiva remain in this place to protect its people. He agrees to both. He stays in the chasm, the brahmins attain liberation, and he is named Mahakaleshvara.
The story explains the name of the deity and the shape of the shrine: a linga that came up out of the earth at a place where devotees, with no other protection, asked their god to stay.
Where this account comes from
- Purana: Shiva Purana, Koti-rudra Samhita 16.1-52 (tr. J. L. Shastri) wisdomlib.org