In a first for Asia, Taiwan's Constitutional Court ruled in favor of same-sex marriage on Wednesday, punctuating a yearslong campaign by advocates for gay rights in one of the continent's most liberal democracies.

In its majority opinion, the court said a provision in the current civil code barring same-sex marriages stood in violation of two articles of the constitution safeguarding human dignity and equality under the law.

Crowds hugged and wept outside the parliament in Taipei. SAM YEH / AFP - Getty Images

Authorities must now either enact or amend relevant laws within two years, failing which same-sex couples could have their marriages recognized by submitting a written document, the court said.

The ruling was greeted with rapturous applause outside the legislature not far from the court in the center of the capital, Taipei, where hundreds had gathered with rainbow flags and noisemakers emblazoned with slogans in favor of gay marriage.

"People like me in the position of being in same-sex relationship with children, we need this law even earlier, even faster," said Jay Lin, a father of two and the founder and director of the Taiwan International Queer Film Festival.

The Morning Rundown Get a head start on the morning's top stories. This site is protected by recaptcha

"And so everybody here and everybody who (is) supporting this law in Taiwan and throughout the world, all be rooting for us. And we will be giving a lot of pressure to the legislators to pass this law once and for all," Lin said.

Jamie, who has been in a relationship with his partner for 22 years, said the ruling was a milestone for Taiwanese society.

"I am so touched. Finally we've reached this moment. This represents Taiwan's human rights. This is a step forward in human rights," said the 60-year-old retiree, who asked that only his first name be used.

A bill to enforce the ruling is already working its way through the legislature, where both the ruling and major opposition parties support legalization of same-sex marriage. Surveys show a majority of the public is also in favor, as is President Tsai Ing-wen, Taiwan's first female leader.

More than 100,000 people took to the streets to celebrate 2016 LGBTQ Pride in Taipei. Craig Ferguson / LightRocket via Getty Images

Gays and lesbians in Taiwan have formed an effective lobby in recent years, with an annual Gay Pride march drawing tens of thousands. While some conservative religious and social groups have mobilized against same-sex marriage, their influence is much less potent than in the United States and many other parts of the world.

"The need, capability, willingness and longing, in both physical and psychological senses, for creating such permanent unions of intimate and exclusive nature are equally essential to homosexuals and heterosexuals, given the importance of the freedom of marriage to the sound development of personality and safeguarding of human dignity," the court said in its ruling.

Two of the court's 15 justices filed dissenting opinions and one recused himself in the case.

Despite the spread of same-sex marriage in a few regions since 2001, gay and lesbian couples had been allowed to marry in only 22 of the world's nearly 200 countries. In Asia, Taiwan is the first government to legalize such unions, while South Africa is the only country in Africa to allow them. More than 70 countries continue to criminalize homosexual activity.

Globally, the pace of civil rights victories has slowed against the background of a steady stream of reports of anti-gay violence and persecution.

Recent weeks have witnessed large-scale detentions of gay men in Nigeria and Bangladesh, and accounts of roundups and torture of scores of gays in Chechnya. In Indonesia, a major police raid on a gay sauna was followed two days later by the public caning of two gay men.

Follow NBC Out on Twitter, Facebook and Instagram