Please make it clear. There must be a solid response. According to the Holy Quran and Sunnah of our beloved prophet Muhammad s.a.w may ALLAH peace and blessings be upon him Ameen. And also Historical . Because it is very painful and it might also disrespect other religious communities and hurt there feeling also .
Constantinople was conquered by force and fighting to the end (فتح عنوة). When this happens to a city, whatever is in it becomes spoils of war - the Muslims gain lawful ownership over it.
فكلوا مما غنمتم حلالا طيبا
So consume what you have taken of war booty [as being] lawful and good
The same applies to immovable property such as buildings, the Quran recounts that the Muslims gained ownership over the settlements of the Banu Nadir, Banu Quraizah and of those at Khyber, which happened through conquest and becoming booty:
وأورثكم أرضهم وديارهم وأموالهم وأرضا لم تطئوها
And He caused you to inherit their land and their homes and their properties and a land which you have not trodden.
So to answer your title question Yes, Hagia Sophia belonged to the Muslims, because it is a part of what they gained through war.
Could the Muslims remove the church, such as by converting it to a Mosque?
According to Islamic law, it is permissible to do so in an area conquered by force. Ibn Taymiyyah (see مسألة في الكنائس page 122-124) notes that there is no difference of opinion on the permissibility of removing churches on territory conquered by combat, rather there is difference of opinion on whether it is obligatory to do so.
That is logical because the Muslims have gained absolute ownership over these buildings and so they may dispense with them as they please. Rather it could be argued that they may be blameworthy for allowing their belongings to be used for kufr and shirk.
There is also specific evidence for this: The Prophet Muhammad ﷺ conquered Mecca and took over the Kaabah for the Muslims exclusively, whereas beforehand it was used by the idolaters as their major temple and place of pilgrimage. Similarly he commanded for a Mosque to be build over a temple at Taif and over a Church. And there are also several other examples which you can find in seerah regarding what was done with the temples of the idolaters. Similarly Umar ibn Abdul Aziz and Harun al-Rashid are reported to have taken churches.
That is notwithstanding that there are also many examples of where churches were allowed to be retained in the lands conquered during the time of the Prophet ﷺ and the sahaba, such as can be found in the cities of Shaam.
Some people hold a misconception that the terms of agreement that the Prophet Muhammad ﷺ and Umar ibn al-Khatab made with the Christians is applicable here. The error in this reasoning is that those people had been annexed peacefully by a treaty, whereas these people had to be fought and subdued via force. The laws are very different for these two cases.
It is in the case of surrender by treaty (فتح صلحا) that the inhabitants become ahl al-dhimma, their properties are granted protection and it is illegal to interfere with their churches, rather the Muslims are responsible for their defense. This exactly was the matter with Jerusalem as its people surrendered to Umar ibn al-Khatab and it was a condition in the treaty that their churches would not be harmed. This case is for those who avert war and agree on a truce:
فإن هم أبوا فسلهم الجزية فإن هم أجابوك فاقبل منهم وكف عنهم
If they refuse, then ask them to pay Jizyah. If they respond, then accept it from them and refrain from (fighting) them.
Whereas the other case is when they are subjugated by the fighting (فتح عنوة) after refusing Jizyah and choosing war. Here their properties become booty and (depending on the madhab) it is left at the discretion of the Imam to act according to what serves the interest of the Muslims: whether to be benevolent and let them retain their properties or to to distribute them among the soldiers or to use them as an endowment for the Muslims.
Reference: معابد الكفار وأحكامها في بلاد المسلمين
Aya Sofia belongs to the Ummah. It was not taken by force, or appropriated unjustly from the Christians - it was bought and paid for with the personal funds of Sultan Muhammad al-Fatih, may Allah have mercy on him, who then left it to the ummah as a waqf, and the actual purchase document still exists :
That's why it is allowed to be converted to a mosque - the patriarch of Constantiople sold it and the building had nothing more to do with Christianity since then. This purchase was at the heart of the recent court case in Ankara, as the link shows.
In Islam, it is forbidden to take over other places of worship by force. Umar ibn Al Khattab RA refused to even pray in the Church of the Holy Sepulchure for fear of setting a precedent where Muslims would take over other places of worship.
But this does not apply to Aya Sofia because it was sold to the Muslims by the Christians. Compare that to the Alhambra in Al Andalus or the Babri mosque in India which were taken by force and converted to a church and Hindu temple respectively.