The origins of Hagia Sophia date back to a church completed on the site on February 15, 360 AD, under Roman Emperor Constantius II. It was called Megali Ekklisia (Great Church / Μεγάλη Ἐκκλησία) and replaced Hagia Eirini as the cathedral of Constantinople. The church burned down during riots protesting the exile of the Patriarch of Constantinople, John Chrysostom, 404.
The second church built on the site was called Hagia Sophia. It was consecrated on October 10, 415, under Theodosius II. This church also burned down on January 13-14, 532, during the Nika Revolt. Over 30,000 people died and half the city burned after an attempt to overthrow Justinian I. Fragments of the church are on display in an excavation area on the west side of the current building.
Justinian decided to rebuild Hagia Sophia much larger and grander than the previous church. He called on architects Isidore of Miletus and Anthemios of Tralles to oversee the construction. Over 10,000 workers were employed. Five years later, on December 27, 537, the new church was inaugurated. When the church was completed, Emperor Justinian proclaimed, “Solomon, I have outdone thee!” (Νενίκηκά σε Σολομών!). It was the largest church in the world until the Cathedral of Sevilla was built in 1520. The full name was the Church of the Holy Wisdom of God (Ναός τῆς Ἁγίας τοῦ Θεοῦ Σοφίας).
After a series of earthquakes over nearly 800 years, along with damage sustained during the Latin occupation of Constantinople (1204-1261), Emperor Andronikos II Palaiologos ordered four buttresses to be built to support the structure. They were completed in 1317. There are 24 buttresses in total that were built during both Byzantine and Ottoman times.
On May 29, 1453, Constantinople was conquered by the Ottomans and Hagia Sophia was converted into a mosque. The first minaret was ordered by Mehmet the Conqueror and built of wood shortly after the building’s conversion to a mosque, and no longer exists. A red brick minaret was built around 1481 on the southeast corner, probably by Beyazıt II. Selim II had another minaret built on the northeast corner, while Murat III had the northwest and southwest minarets built. The three minarets are made of white limestone and sandstone and were designed by legendary Ottoman architect Mimar Sinan, who also strengthened the building in anticipation of earthquakes. Although the building functions as a museum, the minarets, since 2013, announce the call to prayer twice every afternoon.
Other structures on the ground of Hagia Sophia include an elementary school built by Mahmut I in 1740, a muvakkithane (timing room) built in 1853 by the Fossati brothers under Abdülmecid I, an almshouse built in 1743 under Mahmut I, and a Byzantine treasury building. A Byzantine baptistry now holds the tombs of two sultans.