Cheers erupted in the Senate chambers after the 25-22 vote was read out loud and the group of seven gay and lesbian lawmakers from the House of Delegates rushed to the middle of the floor to embrace supportive senators.

O'Malley, a Democrat, shook hands with activists after the vote. "This has been a difficult issue for many people," he said. "But as one Maryland we came together around the principal of human dignity. … It does feel good to have this issue resolved."

The vote makes Maryland the eighth state to approve gay nuptials — and the fourth state legislature to do so in the past 12 months.

Underscoring how the debate has put Maryland in the national spotlight, reaction came swiftly from U.S. House Democratic Leader Nancy Pelosi, who called it "an extraordinary victory," and several D.C.-based gay rights groups.

The approval in Annapolis caps years of failed attempts by gay-rights advocates to gain equal access to marriage and the legal protections that accompany the union. The governor said he plans to sign the legislation next week.

Implementation of the measure is far from certain. Even supporters concede that the law will likely be petitioned to referendum, and they expect Maryland voters to have the final say in November. The legislation has an effective date of January 2013 — well after the November election.

"We move on to the next phase," said Senate Republican leader E.J. Pipkin, who tried to defeat the bill. "There will be a robust referendum effort." Recent polls have shown that Marylanders are evenly split on the issue, so it is difficult to predict what will happen at the ballot box.

Still, supporters rejoiced. Among those who came to witness the vote from the Senate galleries — and applaud its outcome — were Katie Fitzsimmons and Coley Haywood of Annapolis, a gay couple who would like to marry.

Fitzsimmons, 32, said she was overjoyed. "I can't explain the emotion. I was smiling and then I was crying," she said.

The pair, who have been a couple for four years, said they have waited to get married until they could do so in Maryland. If the law survives the expected referendum, "we'll get married right away," Fitzsimmons said. "It's a game-changer. This changes our lives forever," she said.

"We can be equal with everyone else," said Haywood, 31. "Our friends and family can't wait."

Just three years ago, the same-sex marriage effort in Maryland seemed stalled because a key Senate committee lacked the votes needed to move a bill to the floor. That changed after the 2010 election, when the chamber picked up two more Democrats and Senate President Thomas V. Mike Miller reconfigured the membership of all committees.

Miller never supported the bill, but he allowed it to come to the floor and worked to ensure that a filibuster could be cut off if needed. He took the unusual step of explaining his red vote from the rostrum.

"I don't think this is civil rights, but this is history," he said.

"Am I on the wrong side of history?" Miller asked. "As a historian, there is no doubt about it. But I understand that and I'll deal with that in my own mind. I believe marriage is between a husband and a wife, and that is why I voted the way I did," Miller said.

Opponents were initially caught off guard last year when a same-sex marriage bill cleared the Senate, but activists — primarily from black churches and the Maryland Catholic Conference — put their forces together. The bill stalled in the House of Delegates; it was pulled from the floor when supporters realized they did not have the votes for passage.

This year, the governor put his name, and the full force of his office, behind the bill. He worked closely with a progressive coalition and House Democratic leaders to find the votes needed.

The measure was approved in the House last Friday with a single vote to spare after a dramatic week in which four lawmakers switched their position to favor the bill.

The Senate reacted rapidly — shooting the House version of the bill through committee in a brief voting session Tuesday and putting it on the floor Wednesday. The chamber took the rare step of debating and passing the bill in a single day Thursday.

The sharpest moment of the morning debate came when Sen. Bryan Simonaire, an Anne Arundel Republican who opposes the bill, stood and read the children's book "King & King," which he said has been used in Massachusetts to teach young children about homosexual relationships.

In the book, a prince rejects a series of princesses, but finally finds love and marries another prince. Simonaire showed the body the final page, where two princes kiss. The senator noted that the two kiss "on the lips."

Simonaire said he fears that such a book could be taught in Maryland schools.

Several senators objected — in Maryland, the General Assembly does not dictate curriculum; school boards do. Sen. Delores G. Kelley, a Baltimore County Democrat, noted that she participates on a curriculum task force and the book is "nothing that I've ever seen before."

Others noted that parents in Maryland participate in the curriculum development process and can pull their children from some classes if they object to the content.

The argument will likely be replayed multiple times if the referendum does go forward. In other states, ads showing that gay marriage will be "taught in schools" have been effective in defeating similar laws at the ballot box.