History of the temple
According to popular tradition documented by J. Gordon Melton, the first Shiva temple at Somnath is believed to have been built at some unknown time in the past. The second temple is said to have been built at the same site by the "Yadava kings" of Vallabhi around 649 CE. In 725 CE, Al-Junayd, the Arab governor of Sindh is said to have destroyed the second temple as part of his invasions of Gujarat and Rajasthan. The Pratihara Gurjar king Nagabhata II is said to have constructed the third temple in 815 CE, a large structure of red sandstone.
However, there is no historical record of an attack on Somnath by Al-Junayd. Nagabhata II is known to have visited tirthas in Saurashtra, including Someshvara (the Lord of the Moon), which may or may not be a reference to a Shiva temple because the town itself was known by that name. The Chaulukya (Solanki) king Mularaja possibly built the first temple at the site sometime before 997 CE, even though some historians believe that he may have renovated a smaller earlier temple.
Ruined Somnath temple, 1869
In 1024, during the reign of Bhima I, the prominent Turkic Muslim ruler Mahmud of Ghazni raided Gujarat, plundering the Somnath temple and breaking its jyotirlinga. He took away a booty of 20 million dinars. Historians expect the damage to the temple by Mahmud to have been minimal because there are records of pilgrimages to the temple in 1038, which make no mention of any damage to the temple. However, powerful legends with intricate detail developed in the Turko-Persian literature regarding Mahmud's raid, which "electrified" the Muslim world according to scholar Meenakshi Jain. They later boasted that Mahmud had killed 50,000 devotees who tried to defend the temple, a formulaic figure.
The temple at the time of Mahmud's attack appears to have been a wooden structure, which is said to have decayed in time (kalajirnam). Kumarapala (r. 1143–72) rebuilt it in "excellent stone and studded it with jewels," according to an inscription in 1169.
During its 1299 invasion of Gujarat, Alauddin Khalji's army, led by Ulugh Khan, defeated the Vaghela king Karna, and sacked the Somnath temple. Legends in the later texts Kanhadade Prabandha (15th century) and Khyat (17th century) state that the Jalore ruler Kanhadadeva later recovered the Somnath idol and freed the Hindu prisoners, after an attack on the Delhi army near Jalore. However, other sources state that the idol was taken to Delhi, where it was thrown to be trampled under the feet of Muslims. These sources include the contemporary and near-contemporary texts including Amir Khusrau's Khazainul-Futuh, Ziauddin Barani's Tarikh-i-Firuz Shahi and Jinaprabha Suri's Vividha-tirtha-kalpa. It is possible that the story of Kanhadadeva's rescue of the Somnath idol is a fabrication by the later writers. Alternatively, it is possible that the Khalji army was taking multiple idols to Delhi, and Kanhadadeva's army retrieved one of them.
The temple was rebuilt by Mahipala I, the Chudasama king of Saurashtra in 1308 and the lingam was installed by his son Khengara sometime between 1331 and 1351. As late as the 14th century, Gujarati Muslim pilgrims were noted by Amir Khusrow to stop at that temple to pay their respects before departing for the Hajj pilgrimage. In 1395, the temple was destroyed for the third time by Zafar Khan, the last governor of Gujarat under the Delhi Sultanate and later founder of Gujarat Sultanate. In 1451, it was desecrated by Mahmud Begada, the Sultan of Gujarat.
By 1665, the temple, one of many, was ordered to be destroyed by Mughal emperor Aurangzeb. In 1702, he ordered that if Hindus revived worship there, it should be demolished completely.
Proclamation of the Gates' incident during the British period
The Gates from the tomb of Mahmud of Ghazni stored in the Arsenal of Agra Fort – Illustrated London News, 1872
In 1842, Edward Law, 1st Earl of Ellenborough issued his Proclamation of the Gates, in which he ordered the British army in Afghanistan to return via Ghazni and bring back to India the sandalwood gates from the tomb of Mahmud of Ghazni in Ghazni, Afghanistan. These were believed to have been taken by Mahmud from Somnath. Under Ellenborough's instruction, General William Nott removed the gates in September 1842. A whole sepoy regiment, the 6th Jat Light Infantry, was detailed to carry the gates back to India in triumph. However, on arrival, they were found not to be of Gujarati or Indian design, and not of Sandalwood, but of Deodar wood (native to Ghazni) and therefore not authentic to Somnath. They were placed in the arsenal store-room of the Agra Fort where they still lie to the present day. There was a debate in the House of Commons in London in 1843 on the question of the gates of the temple and Ellenbourough's role in the affair. After much crossfire between the British Government and the opposition, all of the facts as we know them were laid out.
In the 19th century novel The Moonstone by Wilkie Collins, the diamond of the title is presumed to have been stolen from the temple at Somnath and, according to the historian Romila Thapar, reflects the interest aroused in Britain by the gates. Her recent work on Somnath examines the evolution of the historiographies about the legendary Gujarat temple