Shiva appears to defeat the enemies of the pious king Vrishabhsen and then agrees to permanently reside in the ancient city of Avantika (now Ujjain).
Mahakaleshwar is one of the Dwadasa Jyotirlingas. Mahakaleshwar is the avatar of Shiva as the Lord of Time and Death. The shrine is situated in the heart of the ancient city of Ujjain next to the Rudra Sagar Lake.
The temple is a five-storey structure with the lowest storey containing the garbhagriha below ground level. Here the lingam of Mahakaleshwar has been installed. The lingam is “dakshinamurthy”, that is, it faces the south. This is the only Jyotirlinga that faces south. The images of Ganesh, Parvati, Kartikeya and Nandi are also installed at this level. The idol of Omkareshwar is installed directly above and that of Nagchandreshwar on the third story. The temple of Nagchandreshwar is open only on Nagpanchami.
The ritual of Bhasm (ashes) Arti is unique to Mahakaleshwar. Hot ashes from crematoriums are smeared on the lingam. Shiva often dwelt in cremation grounds and the ritual pays homage to this aspect of Shiva, which is in keeping with his avatar as the Lord of Time and Death. The Bhasm Arti is held between 4 am and 6 am every morning. Only males wearing silk cloth are permitted in the garbhagriha during this period. Two other festivities are associated with Mahakaleshwar. On the last Monday of the holy month of Sravana the idol of Mahakaleshwar is carried in a palanquin through the city. Mahashivratri is a major festival when a fair is held and the worship continues through the night.
In ancient times the city of Ujjain was known as Avantika. Once an ardent devotee of Shiva, Vrishabhsen ruled the city. The neighboring kings were envious of the riches of Ujjain and decided to launch an attack together and ransack the city. They sought the help of an asura named Dushan, who had been given the boon of being invisible by Brahma. They let mayhem loose in the city. A boy by the name of Shrikhar and a priest by the name of Vridhi led prayers to Shiva. The Lord heard their pleas and appeared from the ground in the avatar of Mahakaleshwar and vanquished the neighboring kings along with Dushan. Shrikhar and Vridhi further requested Shiva to reside permanently in Ujjain as the patron and protector of the city. On its own the lingam of Mahakaleshwar was formed and riding a beam of light Shiva entered the lingam.
Ujjain was the capital city of the Gupta kings. Kalidas, the famous Sanskrit playwright, was closely associated with this dynasty. In many of his works the Mahakaleshwar temple is referred to in reverent terms. The Tamil hymns of the Nayanmar saints also mention the temple.
The ancient shrine was first renovated in the 11th century. But soon thereafter, in 1234, Iltutmish, the Muslim ruler of Delhi, destroyed the temple. The present shrine was renovated during Maratha rule about 250 years ago by Baba Ramchandra Shenvi, the Divan of the Scindia state. Some parts of the earlier temple were retained. The dome appears to have been constructed more recently.
Please check my course on Hindu Mythology
Please check my articles on Mythology from India