TL;DR: There's no one correct Islamic answer to this: it's incredibly nuanced and scholarly opinions are evolving.
Iran has relatively long history of accepting transgender people, but even its laws regarding transgender marriage are not clear. Pakistan has brought in new transgender rights laws, and scholars in countries like Egypt and Malaysia are calling for scholarly opinions on transgender people to be revised.
I expect most countries would formally follow laws based on what's on your identity documents (one's "legal gender"), which is affected by local laws (not necessarily at a national level).
There's two main camps:
Traditionally, the Islamic stance has been you are what your genitals at birth indicate, and that's that. All rulings apply as per your birth sex. (E.g. ...the person appears outwardly to be female, but in fact he is a male Islam Q&A.) The usual supporting evidence is:
... I will command them so they will change the creation of Allah ...
However, this doesn't apply to e.g. circumcision or medically necessary procedures, so applying this requires care.
Allah's Messenger cursed those men who are in the similitude (assume the manners) of women and those women who are in the similitude (assume the manners) of men.
Sahih al-Bukhari 5885
But is e.g. a male-to-female transgender person imitating a woman if they transition, or are they imitating a man if they don't transition?
Two ahadith about mukhannath or "effeminate ones":
A mukhannath (eunuch) used to enter upon the wives of Prophet. They (the people) counted him among those who were free of physical needs. One day the Prophet entered upon us when he was with one of his wives, and was describing the qualities of a woman, saying: When she comes forward, she comes forward with four (folds in her stomach), and when she goes backward, she goes backward with eight (folds in her stomach). The Prophet said: Do I not see that this (man) knows what here lies. Then they (the wives) observed veil from him.
Sunan Abi Dawud 4107 [grade: sahih]
This one suggests the Prophet was originally comfortable with the mukhannath around his wives, until he indicated attraction to females.
Am effeminate man (mukhannath) who had dyed his hands and feet with henna was brought to the Prophet. He asked: What is the matter with this man? He was told: "Messenger of Allah! He imitates the look of women." So he issued an order regarding him and he was banished to an-Naqi'. The people said: Messenger of Allah! Should we not kill him? He said: I have been prohibited from killing people who pray. AbuUsamah said: Naqi' is a region near Medina and not a Baqi'.
Sunan Abi Dawud 4928 [grade: sahih]
This last hadith is the strongest against being transgender, indicating banishment.
Nowadays, while some scholars maintain traditional views, others are more nuanced.
Sheikh Majdi Ashour, academic advisor to the Grand Mufti of Egypt, stressed that it is not possible to take the opinion of religion on this thorny issue in general, as each case has its own conditions which cannot be generalized to other cases.
Transgender in Egypt: Islam’s stance on sex reassignment surgery, Egypt Independent, 2018
What seems to be swaying some modern scholars are:
So-called mukhannath min kalqin reported by An-Nawawi, who identify as female and are sexually attracted exclusively to men:
According to religious scholar, Imam An-Nawawi, there exist two types of Mukhannaths ... Mukhannath min Kalqin were born male by body but identified their gender as female, and they are not attracted to women. ... Since their identity is inert and not an impersonation, as long as they do not use their identity for immoral purposes, it is not a shame and they are accepted in Islam.
Malaysian Federal Territories Mufti Datuk Zulkifli Mohamad al-Bakri, FT Mufti backs call to revisit edict against transgenders, 2018.
(Wikipedia gives another example by Ibn 'Abd al-Barr: This is one of those who have no interest in women who were permitted to enter upon women.)
Gender identity disorder (or gender dysphoria) is a recognized medical condition, and its treatment is regarded as medically necessary (on a case-by-case basis):
Medical treatment ... are effective in alleviating gender dysphoria and are medically necessary for many people.
WPATH Standards of Care V7 (pdf)
Thus, medically transitioning is not through some personal whim, but through medical professionals.
Failing to accept transgender identities may realistically lead to suicide or apostasy, considered among the worse sins in Islam, and possibly even sodomy.
There's a growing body of evidence of a biological basis; see Causes of transsexuality on Wikipedia.
Consequently, Pakistan has passed new transgender rights laws (The Transgender Persons (Protection of Rights) Bill (pdf)) that makes Western transgender rights laws look weak. This comes after news of a fatwa declaring "marriage with a transgender person is lawful".
The main Islamic country which funds transgender operations is Iran (see Transsexuality in Iran, Wikipedia). There's a news report of transgender people marrying in Iran in 2009, but it required a court ruling.