The Senate on Wednesday took a crucial step toward passing landmark legislation to provide federal protections for same-sex marriages, as 12 Republicans joined Democrats to advance the Respect for Marriage Act, putting it on track to become law in the twilight of the Democratic-held Congress.

The 62-37 vote, which came only days after the midterm elections in which Democrats retained control of the Senate but were on track to lose the House to Republicans, was a rare and notable last gasp of bipartisanship by a lame-duck Congress as lawmakers looked toward an era of political gridlock in a divided Washington.

It also signaled a remarkable shift in American politics and culture, demonstrating how same-sex marriage — once a politically divisive issue — has been so widely accepted in society that a law to protect the rights of same-sex couples across the country could gain decisive, bipartisan majorities in both the Senate and the House. In July, 47 House Republicans joined Democrats to pass a version of the bill.

Sen. Chuck Schumer of New York, the majority leader, said that passage of the legislation — now expected after Thanksgiving — would be “one of the true highlights of the year for this body” and “one of the more significant accomplishments of this Senate to date.”

Speaking on the Senate floor, Schumer noted that his daughter and her wife were expecting a baby in the spring and he wanted “them, and every one in a loving relationship, to live without the fear that their rights could one day be stripped away.”

Even as the test vote reflected bipartisan support for the measure, the vast majority of Republicans — including Sen. Mitch McConnell of Kentucky, the minority leader — voted against it. Thirty-seven Senate Republicans voted no, illustrating that, while polling has found that more than 70% of Americans — including a majority of Republicans — support same-sex marriage, the issue remains politically untouchable for many GOP lawmakers.

The bill would not require any state to allow same-sex couples to marry. But it would repeal the Defense of Marriage Act, which denied federal benefits to same-sex couples. The push to bring it up for a vote began over the summer, after Justice Clarence Thomas suggested in his opinion in the ruling that overturned the 50-year-old Roe v. Wade decision that established abortion rights that the court also “should reconsider” past rulings that established marriage equality and access to contraception.

Some Republicans balked at the move, arguing that marriage-equality rights were not under any immediate threat and that there was no urgency to pass legislation to safeguard those protections.

But Sen. Tammy Baldwin, D-Wis., the lead sponsor of the legislation, said Wednesday that gay people were “scared for good reason” and that the same legal arguments the Supreme Court rested on to reverse Roe could just as easily be applied to other cases.

“The Supreme Court should not be in a position to undermine the stability of families with the stroke of a pen,” said Baldwin, the first openly gay woman to be elected to the House and the Senate, noting that Thomas in his dissent “was essentially providing an open invitation to litigators across the country to bring their cases to the Supreme Court.”

Sen. Susan Collins, R-Maine, one of the 12 Republican supporters of the bill, added that even without an imminent threat, “there is still value in ensuring that our federal laws reflect that same-sex and interracial couples have the right to have their marriages recognized regardless of where they live in this country.”

The successful vote Wednesday marked an improbable outcome for a measure that was once regarded as a symbolic act by Democrats to show their support in the face of solid Republican opposition that would block it from clearing Congress.

Democrats had initially taken up the measure as an election-year maneuver to show voters that they were doing everything possible to protect same-sex marriage rights in the face of new threats from a conservative Supreme Court. Instead, it passed in the House with 47 Republicans joining Democrats in favor, and a bipartisan group in the Senate began talks on a version that could draw enough Republican backing in that chamber to move forward.

They agreed to add language ensuring that churches, universities and other nonprofit religious organizations could not lose tax-exempt status or other benefits for refusing to recognize same-sex marriages and could not be required to provide services for the celebration of any marriage. They also added language to make clear that the bill does not require or authorize the federal government to recognize polygamous marriages.

The bill now returns to the House, which must pass the revised version before clearing it for President Joe Biden’s signature.

The Republican senators who ultimately voted for the measure were: Roy Blunt of Missouri, Richard Burr of North Carolina, Shelley Moore Capito of West Virginia, Susan Collins of Maine, Cynthia Lummis of Wyoming, Rob Portman of Ohio, Dan Sullivan of Alaska, Mitt Romney of Utah, Thom Tillis of North Carolina, Lisa Murkowski of Alaska, Joni Ernst of Iowa and Todd Young of Indiana.