Hagia Sophia was initially built as a Christian cathedral in the 6th century in the Byzantine Empire's capital of Constantinople (modern-day Istanbul, Turkey). 

In 1204, Constantinople was conquered by the Western European crusaders during the Fourth Crusade, and the cathedral was converted into a Roman Catholic cathedral. 

The Byzantine Empire retook Constantinople in 1261, and the cathedral was returned to its original function as an Eastern Orthodox cathedral.

In 1453, Constantinople was conquered by the Ottoman Empire, and the cathedral was converted into a mosque. The Ottomans added minarets, a mihrab (a niche in the wall indicating the direction of Mecca), and other features to the building to make it more suitable for use as a mosque. 

The building served as a mosque until 1935 when it was converted into a museum by the Turkish government.

In 2020, the Turkish government announced that it would again be used as a mosque. The decision to convert the building into a mosque sparked controversy and debate, as some argued that it should remain a museum due to its historical and cultural significance.