According to the legend,
Soma or the Moon God built the temple in gold,
Ravana in silver, and Shri
Krishna in wood. Soma was cursed by his father-in-law
Daksha to wane because Soma loved only one of his wives, all of whom happened to be Daksha's daughters. His other wives complained about this negligent behavior of Soma to their father Daksha, and thus the curse. He then built a Shivlinga at the Prabhas tirth (a Hindu pilgrimage) and prayed to Lord
Shiva who removed the curse partially because asked upon by Soma's one wife (the one that he loved more than others). Thus, causing the periodic waning of moon.Pleased by the prayers Soma (Moon god), Lord Shiva decided to rest in that
Lingam till eternity, and thus the
Jyotirlingam.
Timeline
The first temple of Somnath is said to have existed before the beginning of the common era.
The second temple, built by the
Yadava kings of
Vallabhi in Gujarat, replaced the first one on the same site around 649 CE.
In 725 CE Junayad, the
Arab governor of
Sind, sent his armies to destroy the second temple.The
Gurjara Pratihara king
Nagabhata II constructed the third temple in
815, a large structure of red sandstone.
In 1296 CE, the temple was once again destroyed by Sultan Allauddin Khilji's army.According to Taj-ul-Ma'sir of Hasan Nizami, Raja Karan of Gujarat was defeated and forced to flee, "fifty thousand infidels were dispatched to hell by the sword" and "more than twenty thousand slaves, and cattle beyond all calculation fell into the hands of the victors". The temple was rebuilt by Mahipala Deva, the Chudasama king of Saurashtra in 1308 AD and the Linga was installed by his son Khengar sometime between 1326 and 1351 AD. In 1375 CE, the temple was once again destroyed by Muzaffar Shah I, the Sultan of Gujarat.
In 1451 CE, the temple was once again destroyed by Mahmud Begda, the Sultan of Gujarat.
In 1701 CE, the temple was once again destroyed by Mughal Emperor Aurangzeb. Aurangzeb built a mosque on the site of the Somnath temple, using some columns from the temple, whose Hindu sculptural motifs remained visible.
Later on a joint effort of Peshwa of Pune, Raja Bhonsle of Nagpur, Chhatrapati Bhonsle of Kolhapur, Queen Ahilyabai Holkar of Indore & Shrimant Patilbuwa Shinde of Gwalior rebuilt the temple in 1783 AD at a site adjacent to the ruined temple which was already converted to a mosque.