According to the Puranas, the city of Ujjain was called Avantika and was famous for its beauty and its status as a devotional epicenter. It was also one of the primary cities where students went to study holy scriptures. According to legend, there was a ruler of Ujjain called Chandrasena, who was a pious devotee of Shiva and worshiped him all the time. One day, a farmer's boy named Shrikhar was walking on the grounds of the palace and heard the King chant the Shiva's name and rushed to the temple to start praying with him.
However, the guards removed him by force and sent him to the outskirts of the city near the river Kshipra. Rivals of Ujjain, primarily King Ripudamana and King Singhaditya of the neighboring kingdoms decided to attack the Kingdom and take over its treasures around this time. Hearing this, Shrikhar started to pray and the news spread to a priest named Vridhi. He was shocked to hear this and upon the urgent pleas of his sons, started to pray to Shiva at the river Kshipra. The Kings chose to attack and were successful; with the help of the powerful demon Dushan, who was blessed by Brahma to be invisible, they plundered the city and attacked all the devotees of Shiva.
Upon hearing the pleas of His helpless devotees, Shiva appeared in his Mahakala form and destroyed the enemies of King Chandrasena. Upon the request of his devotees Shrikhar and Vridhi, Shiva agreed to reside in the city and become the chief deity of the Kingdom and take care of it against its enemies and to protect all His devotees. From that day on, Shiva resided in His light form as Mahakala in a Lingam that was formed on its own from the powers of Shiva and his consort, Parvati. Shiva also blessed his devotees and declared that people who worshipped Him in this form would be free from the fear of death and diseases. Also, they would be granted worldly treasures and be under the protection of the Shiva himself.
Bharthari was the elder son of King Gandharva-Sena, and received the kingdom of Ujjain from the celestial god Indra and the King of Dhara.When Bharthari was king of 'Ujjayani' (modern-day Ujjain) in his state there lived a Brahman who after years of austerities was given the fruit of immortality from the celestial tree of Kalpavriksha. The Brahman presented the same to his monarch, Raja Bharthari, who in turn, passed it on to his love, the beautiful, Pinglah Rani or Ananga Sena Raja Bhartrhari's last and youngest wife.
The queen, being in love with the Head police officer of the state, Mahipaala, presented the fruit to him, who further passed it on to his beloved, Lakha, one of the maids of honour. Eventually, Lakha being in love with the king presented the fruit back to the king. Having completed the circle, the fruit revealed the downsides of infidelity to the king, he summoned the queen and ordered her beheading, and ate the fruit himself. After that, he abdicated the throne, and became a religious mendicant.
He later became a disciple of Pattinatthar who first indulged in an argument about samsari and sanyasi with king Bhartrhari. Later during the conversation pattinathar said that all women have 'dual mind' and it might be the true case even with Parameswari. King conveyed this news to Rani Pingalah and she ordered Pattinathar to get punished and to sit in kalu maram (tree, whose top portion would be sharpened like a pencil and whole tree is fully coated with oil, a person who is punished to sit in the top will be split into two pieces), they tried to kill Pattinathar, but Kalu Maram started burning and nothing happened to Pattinathar, the king received the news and went directly to Pattinathar and asked him to get ready to die the next day, but Pattinathar replied, "I'm ready right now, to die". The next day king came with tears in his eyes and released saint from jail because he actually noticed Queen Pingalah in love with horsemen that night, He threw away his empire, wealth, even full coat dress and dressed in a simple kovanam (loincloth), the king became a disciple of Pattinatthar and got moksha (salvation) in the Srikalahasteeshwara Temple in Andhra Pradesh which houses the Vayu Lingam, a part of the Pancha Bhoota Sthalams of Shiva.
Kalidasa (c. 2nd–1st century BCE), the great Sanskrit poet of the times who was probably a contemporary of king Pushyamitra Sunga, has mentioned about the rituals of the temple in his works in Meghadūta. He mentions about the nada-aradhana, the performance of art and dance during the evening rituals.
The Mahakaleshwar Temple as a Shakti Peeth
Shakti Peethas are shrines that are believed to have enshrined with the presence of Shakti due to the falling of body parts of the corpse of Sati Devi, when Shiva carried it. Each of the 51 Shakti Peethas has shrines for Shakti and Kalabhairava. The Upper Lip of Sati Devi is said to have fallen here and the Shakti is called as Mahakali