Maryland will join seven states and the District in allowing same-sex marriage, ending a year-long drama in Annapolis over the legislation and expanding nationwide momentum for gay rights.

The Senate passed the measure by a vote of 25 to 22 Thursday night, and Gov. Martin O’Malley (D) has vowed to sign it into law.

To win some of the final votes needed for passage in the House of Delegates last week, backers agreed to conditions that could help opponents place the new law on the November ballot. With polls showing the Maryland electorate almost evenly split on the issue, a referendum all but promises another contentious battle before the issue is settled in the state.

Ministers of several African American megachurches in Prince George’s County as well as conservative and Catholic groups have vowed to help repeal the measure.

The likelihood that the issue could land on the November ballot in Maryland presents a potential dilemma for President Obama. He has been heavily courting the gay community for donations and votes in his reelection campaign but has stopped short of fully embracing marriage rights. Obama has said his views are “evolving,” a statement viewed by many supporters in that community as a strong hint that he will soon endorse the cause, perhaps if and when he is safely reelected.

Gay rights activists can be expected to pressure the president to publicly support the Maryland law in November.

At the same time, however, Obama will probably be pressured by many African American leaders in Maryland to join them in opposing the measure.

If the debate in Maryland’s legislature is any guide, the mixture of emotions among African Americans over gay marriage will not be an easy issue for Obama to navigate.

Despite Maryland having one of the largest Democratic majorities in any state legislature, same-sex marriage advocates had to overcome fierce opposition from blocs of African American lawmakers, especially those with connections to large evangelical churches, to cobble together coalitions big enough to pass both chambers.

Maryland has the highest percentage of black voters of any state outside of the Deep South and a percentage of black voters nearly twice that of any other state that has passed same-sex marriage.

David Mixner, a prominent gay rights activist and blogger who is also an Obama campaign donor, said the Maryland bill “lands the issue squarely on the president’s desk” because of the state’s proximity to Washington and its sizable black electorate.

He noted that Obama’s opposition to gay marriage has been cited by opponents in California, as well as by New Jersey Gov. Chris Christie (R), who recently vetoed a marriage law.

“It is essential that he makes clear to the voters of Maryland, without any caveats, that if he were a resident of the state, he would vote against repeal,” Mixner said. “We can’t afford to have his statement be ambiguous so the other side can claim that he’s on their side.”

As the Maryland Senate’s final vote was read aloud just after 6 p.m. in Annapolis, cheers and applause erupted from packed galleries of onlookers. Groups of supporters quickly spilled into the streets in front of the State House as passing motorists honked their horns and flashed a thumbs-up.

O’Malley appeared in the hallway outside the Senate and embraced Sen. Richard S. Madaleno Jr. (D-Montgomery), one of the legislature’s openly gay lawmakers, who has for years been a lead sponsor of the measure.

“It is a dream that I find hard to believe, that this has come true,” Madaleno said on the floor of the Senate moments before the vote. “That in Maryland today, I can stand amongst you, as a gay man, as a married man, as a father, as a state senator, that I and my family have been embraced by part of the family and fabric of Maryland.”

O’Malley had for years publicly opposed gay marriage in favor of civil unions but this year took on the cause as his own, saying his views had evolved. He said he looked forward to signing the bill.

The governor’s rising star in the Democratic Party has been tied in part to passage of the bill, especially because in recent months it appeared that Maryland might be unable to keep pace with New York, where Gov. Andrew M. Cuomo (D) was able to secure passage of a gay marriage bill in a divided state legislature.

Speaking to reporters immediately after the vote, O’Malley said he will campaign vigorously if opponents are able to force a referendum in November. “I wouldn’t sign it if I didn’t believe in it,” he said.

For several religious and Republican groups, however, Thursday’s vote marked just the beginning of what opponents promised will be an all-out effort to ensure Maryland’s electorate gets the final say.

A Web site set up last year by Maryland Republicans to solicit online signatures for just that purpose carried a new message Thursday urging visitors to sign up to make sure Marylanders are allowed to vote on the bill. “Sign up and we’ll let you know when we’re ready to start the petition,” the page reads.

As a condition to win support last week from a handful of African American members of the House, backers agreed to prevent same-sex marriages from occurring in the state until all legal challenges have been exhausted.

Sen. Jamie B. Raskin (D-Montgomery), who led proponents’ efforts on his chamber’s floor, suggested that the requirement would ensure that if opponents collect enough valid signatures, the measure would appear on the ballot.

“We’ve done triple back flips to guarantee that the petition will be able to work its way through,” Raskin said.

Unlike an emotional 12-hour hearing and lengthy debates that included personal appeals from sponsors in the House, the final machinations in the Senate on Thursday were businesslike. The bill first passed the Senate a year ago by a vote of 25 to 21, only to fall short in the House, which had been thought to have been the more liberal of the two chambers.

Proponents spoke sparingly, aiming to shorten debate before a final vote. Opponents attempted surgical changes — sometimes adding just one word — to require the measure to return to the House for a second vote, where it passed with only one vote to spare last week.

Sen. C. Anthony Muse (D-Prince George’s), who is also senior pastor at the Ark of Safety Christian Church of Upper Marlboro, introduced an amendment that failed, saying the bill should have been renamed from the Civil Marriage Protection Act to the Marriage Redefinition Act.

“Should this law go into effect we are going to change the definition of marriage. If that’s what we’re going to do, let’s be clear and call it what it is,” Muse said. “A bill that says ‘protection’ leads one to believe that something that is law is under attack. It is not, it is a redefinition of what we have all understood our marriage to be, between one man and one woman.”

Raskin, a constitutional law professor who served as floor leader for the bill, fought back. He said marriage had been changed many times in Maryland and never branded with such a title.

“There was a law against slaves marrying, and when that was changed, that was not called the marriage redefinition act,” Raskin said. “There was a law against interracial marriage up until 1967, when this chamber voted against it, and that was not called the marriage redefinition act.”

Seven openly gay delegates squeezed into the Senate chamber to watch their colleagues vote on the bill. Del. Heather R. Mizeur (D-Montgomery) said she was “keeping every finger and toe crossed.”

“We have learned with this issue that’s never final until it’s final,” Mizeur said. “As soon as we won, I called up my spouse, Deborah, and asked her if she would be my ‘lawfully’ wedded wife.”

Staff writers Greg Masters and Peter Wallsten and staff researcher Madonna Lebling contributed to this report.