Lawmakers pressured by LGBT groups as well as by church organisations opposed to the move approved most of a government-sponsored bill.
Taiwan's legislature voted on Friday to legalise same-sex marriage, a first in Asia and a boost for LGBT rights activists who had championed the cause for two decades.
Lawmakers, pressured by both LGBT groups and church organisations opposed to the move, approved most of a government-sponsored bill that recognises same-sex marriages and gives couples many of the tax, insurance and child custody benefits available to male-female married couples.
That makes Taiwan the first place in Asia with a comprehensive law both allowing and laying out the terms of same-sex marriage.
Taiwanese President Tsai Ing-wen, a supporter of the law, tweeted: "On May 17th, 2019 in Taiwan, LoveWon. We took a big step toward true equality, and made Taiwan a better country."
"It's a breakthrough, I have to say so," said Shiau Hong-chi, professor of gender studies and communications management at Shih-Hsin University in Taiwan.
Thousands of people, including same-sex couples, demonstrated on Friday morning in the rainy streets outside parliament before the vote. Many carried rainbow-coloured placards reading "The vote cannot fail."
About 50 opponents sat under a tent outside parliament and gave speeches favouring marriage between only men and women.
Taiwan's Constitutional Court in May 2017 said the constitution allows same-sex marriages and gave parliament two years to adjust laws accordingly.
The court order mobilised LGBT advocacy groups pushing for fair treatment, as well as opponents among church groups and advocates of traditional Chinese family values that stress the importance of marriage and producing offspring.
Religion, conservative values and political systems that discourage LGBT activism have slowed momentum toward same-sex marriage in many Asian countries from Japan through much of Southeast Asia, although Thailand is exploring the legalisation of same-sex civil partnerships.
Phil Robertson, deputy Asia director for Human Rights Watch, said Taiwan's action should "sound a clarion call, kicking off a larger movement across Asia to ensure equality for LGBT people and pro-active protection of their rights by governments throughout the region. No more excuses!"
Taiwan's acceptance of gay and lesbian relationships began in the 1990s when leaders in today's ruling Democratic Progressive Party championed the cause to help Taiwan stand out in Asia as an open society.
Although claimed by China as its own territory, Taiwan is a self-governing democracy with a vibrant civil society dedicated to promoting rights for sexual and ethnic minorities, women, the handicapped and others.
Mainland China, ruled by the authoritarian Communist Party, remains much more conservative and officials have repeatedly discouraged even the discussion of legalising same-sex marriage.
Despite that, news of Taiwan's new law was a major trending topic on social media in China, with more than 100 million views on the Twitter-like microblogging site Weibo.
Opponents in Taiwan raised fears of incest, insurance scams and children confused by having two mothers or two fathers.
"This is going to cause a lot of morality problems," said Lin Shih-min with the Taiwan political action group Stability of Power, which opposed the law.
"From the point of view of the children, they have the right to grow up with both a mother and a father."
In November 2018, a majority of Taiwan voters rejected same-sex marriage in an advisory referendum. However, legislators favouring the bill, and voting separately on each item largely along party lines, said it followed the law as well as the spirit of the referendum.
"We need to take responsibility for the referendum last year and we need to take responsibility for people who have suffered from incomplete laws or faced discrimination," ruling party legislator Hsiao Bi-khim said during the three-hour parliament session.
At least 20 same-sex couples are planning a mass marriage registration in Taipei on May 24, a spokesman for the advocacy group Marriage Equality Coalition Taiwan said.
The law will give a boost to Jay Lin and his partner, who hope to marry and assume joint custody of their two 2-year-old sons. They plan to register after May 24.
"A lot of gay parents are excited about that already," said Lin, a Taipei-based online streaming service founder.