Of 98 Democrats in the House, as many as 30 — mostly more-conservative Democrats known as Blue Dogs, and African-Americans from districts where churches are strong — had been undecided.

The bill’s passage was made possible by two Republicans, three Blue Dogs, and two African-American delegates, none of whom were initially supportive. In interviews, several of those delegates said that a key change from last year that won their support was language protecting religious institutions from being forced to perform marriages.

“People believe that it’s a sin for a homosexual to be married, but who are they to judge?” said Robert Costa, one of the Republicans who voted for the bill. “It’s up to God, not government.”

John Olszewski, a Democrat initially opposed, said the religious exemptions made him feel comfortable enough to vote for the bill. “Denying basic rights to folks just isn’t the right thing for us to be doing,” he said.

Several delegates said they had been warned by powerful parts of their constituencies that a yes vote would cost them in the next election, scheduled for 2014. Mr. Mitchell said it was a risk he was willing to take.

“I’ve heard all types of threats, that in 2014 at the ballot box, there would be revenge,” he said. “But when that day comes, I know that for the seven openly gay colleagues, if they are able to have the same rights as my wife and I have, then I know that my green vote was the right vote.”

Opponents said they were confident they could gather the signatures needed to place it on the ballot, about 55,000. If the referendum is successful, it would effectively repeal the bill.