Nicola Sturgeon's government had championed the reforms, which makes it easier for people to change their gender for legal purposes.
The Scottish Parliament has passed a new gender identity law which makes it easier for people -- including 16 and 17-year-olds -- to change their gender for legal purposes
The vote was 86-39 with most of Nicola Sturgeon's Scottish National Party MSPs, plus lawmakers from the Greens, Labour and Liberal Democrats, supporting the new legislation, while the Scottish Conservatives campaigned against the reform.
A number of Sturgeon's SNP politicians broke party unity and voted against the new law, the biggest rebellion against the government by its own party in the last 15 years.
Along the way the bill sparked acrimonious debate, with politicians arguing so late this week in the Edinburgh parliament that the lights in the chamber, set with an automatic timer, went out. Rival groups of protesters have also demonstrated noisily outside the building.
In practice, the new law would allow people to transition by self-declaration, removing the need for a medical diagnosis of gender dysphoria. The government says it is a simple step that will improve the lives of transgender people by allowing them to get official documents that correspond with their gender identity.
Opponents, including Harry Potter author J.K. Rowling, claim the simplified procedure risks allowing predatory men to gain access to spaces intended for women, such as shelters for domestic abuse survivors.
Nicola Sturgeon has said that men didn't have to change their identity and pretend to be women in order to abuse them.
Debates this week became heated when spectators shouted “Shame on you” after lawmakers defeated an amendment that aimed to prevent convicted sex offenders from obtaining a gender recognition certificate.
Legislators also defeated an amendment that would have set the minimum age for gender transition at 18 rather than 16.
And even in the final hour of discussions Thursday, a protester was escorted out after interrupting proceedings.
What were supporters for and against saying?
Supporters of transgender rights demonstrated outside the parliament building in Edinburgh to urge lawmakers to support the bill. One of them, Beth Douglas, said the bill under debate would allow people to have birth certificates -- and eventually their death certificates -- that match their gender identity.
“There will be a day for everyone when we are issued a death certificate,” said Douglas, who runs an LGBT+ group. “When that happens for me, I want that death certificate to be in the right gender. I would like to rest in peace and die in dignity.”
At a separate demonstration by opponents, J.K. Rowling’s husband read out a message from the author, in which she branded the bill “the single biggest rollback of women’s rights in our lifetimes.”
Joanna Cherry, an SNP lawmaker in the UK Parliament in London, said the law "gives any man the right to self-identify as a woman after three months of living as a woman … with minimal safeguards.”
The law will set Scotland apart from the rest of the United Kingdom, where a gender dysphoria diagnosis is needed before individuals can have the gender designation on their identity documents changed.
Under the Scottish legislation, trans people would be allowed to have the markers revised if they have spent three months living with a different expressed gender, six months for people ages 16 and 17. The current period is two years.
Opponents say the legal change could endanger women and girls, who currently are guaranteed access to single-sex spaces such as changing rooms and shelters.
The Scottish government says trans people can still be excluded from single-sex spaces under existing legislation.