“Love has won over hate, and equality has won over discrimination,” Annie Huang, acting director of Amnesty International Taiwan, said in a statement. “This is a moment to cherish and celebrate, but it has been a long and arduous campaign for Taiwan to become the first in Asia to legalize same-sex marriage.”

Taiwan has long been a leader of gay rights in Asia, a region where such rights have lagged, and the annual gay pride parade in Taipei is a magnet for gays and lesbians from countries where discrimination and unequal treatment is far more entrenched. In one of the harshest examples in the region, Brunei this year put into effect new laws that authorized executions by stoning for gay sex and adultery, although the country’s leader said it would maintain a de facto moratorium on the death penalty.

Ms. Tsai, who took office in 2016, said during her campaign that she supported same-sex marriage, and her left-leaning Democratic Progressive Party, which took control of the legislature for the first time that year, also generally favors such legislation.

But momentum for a same-sex marriage law had stalled as opponents, including some church and conservative groups, campaigned against the mandated changes. Voters overwhelmingly opposed same-sex marriage in referendums last year, and politicians have been slow to move forward out of fears of being punished in next year’s general election.

That left the government facing a May 24 deadline. Several gay couples had said they planned to get married on that day, regardless of whether or not the legislature acted.