The US House of Representatives on Thursday gave final congressional approval to legislation that provides federal recognition of samesex marriages, a measure born out of concern that the Supreme Court could reverse its support for legal recognition of such relationships.

The House vote was 258-169, with all of the chamber’s Democrats and 39 Republicans voting in favor – though 169 of the chamber’s Republicans voted against it and one voted “present.” The measure now goes to Democratic President Joe Biden’s desk for signature into law. The Respect for Marriage Act, as it is called, won Senate approval last month.

The legislation won the support of LGBT advocates as well as a number of religious organisations and entities including the Church of Jesus Christ of Latter-day Saints, though many American religious conservatives still oppose gay marriage as counter to biblical scripture.

It is narrowly written to act as a limited backstop for the 2015 Supreme Court decision that legalised samesex marriage nationwide, known as Obergefell v. Hodges. It would allow the federal government and states to recognise samesex and interracial marriages as long as they were legal in the states where they were performed. It makes concessions for religious groups and institutions that do not support such marriages.

The measure would repeal a 1996 US law called the Defence of Marriage Act, which among other things denied federal benefits to samesex couples. It bars states from rejecting the validity of out-of-state marriages on the basis of sex, race or ethnicity. The Supreme Court in 1967 declared prohibitions on interracial marriage unconstitutional.

But the legislation would not bar states from blocking samesex or interracial marriages if the Supreme Court allowed them to do so. It also ensures that religious entities would not be forced to provide goods or services for any marriage and protects them from being denied tax-exempt status or other benefits for declining to recognise samesex marriages.

In a speech on the House floor ahead of the vote, Democratic Speaker Nancy Pelosi condemned the “hateful movement” behind attacks on LGBT rights in the United States.

The legislation “will help prevent right-wing extremists from upending the lives of loving couples, traumatising kids across the country and turning back the clock on hard-won prises,” Pelosi said.

Republican Representative Jim Jordan said the bill was “dangerous and takes the country in the wrong direction.”

When the Senate passed it by a vote of 61-36, 12 Republicans joined 49 Democrats in supporting it. Most Senate Republicans voted against it.

A broader version of the bill – without the explicit protections for religious liberty – passed the 435-seat House in August, with the backing of all the Democrats and 47 Republicans. But to get the necessary 60 votes in the Senate to proceed with the legislation amid opposition by many Republican senators, its co-sponsors added an amendment clarifying that religious groups could not be sued under the it.

The legislation was written by a group of Democratic and Republican senators in response to fears that the Supreme Court, with its increasingly assertive conservative majority, could someday strike down the Obergefell ruling, potentially jeopardising samesex marriage nationwide. The court has shown a willingness to reverse its own precedents as it did in June when it overturned its landmark 1973 ruling that had legalised abortion nationwide.

The Supreme Court’s conservative majority on Monday appeared ready to rule that a Christian web designer has a right to refuse to provide services for samesex marriages in a case the liberal justices said could empower certain businesses to discriminate based on constitutional free speech protections.

About 568,000 married samesex couples live in the United States, according to the US Census Bureau.