(Reuters) - New York's state legislature gave final approval on Friday to same-sex marriages, a key victory for gay rights ahead of the 2012 presidential and congressional elections.

New York will become the sixth and most populous state to allow gay marriage. State senators voted 33-29 to approve marriage equality legislation introduced by Governor Andrew Cuomo, a Democrat in his first year of office.

"New York has finally torn down the barrier that prevented same-sex couples from exercising the freedom to marry and from receiving the fundamental protections that so many couples and families take for granted," Cuomo said in a statement.

After Cuomo signs the bill into law, same-sex weddings can start taking place in New York in 30 days, though religious institutions and nonprofit groups with religious affiliations will not be compelled to officiate at such ceremonies.

"I have to define doing the right thing as treating all persons with equality and that equality includes within the definition of marriage," said Republican Senator Stephen Saland, speaking before the bill was passed.

Cheers erupted in the Senate gallery in the state capital Albany and among a crowd of several hundred people who gathered outside New York City's Stonewall Inn, where riots following a police raid in 1969 sparked the modern gay rights movement.

"It's about time. I want to get married. I want the same rights as anyone else," Caroline Jaeger, 36, a student, who was outside the Stonewall Inn.

But New York's Catholic bishops said they were "deeply disappointed and troubled" by the passage of the bill.

"We always treat our homosexual brothers and sisters with respect, dignity and love. But we just as strongly affirm that marriage is the joining of one man and one woman," the New York State Catholic Conference said in a statement.

New York City Mayor Michael Bloomberg, an advocate for gay marriage who lobbied state lawmakers in recent weeks, said the vote was an "historic triumph for equality and freedom."

"Together, we have taken the next big step on our national journey toward a more perfect union," he said in a statement.

ELECTION ISSUE

President Barack Obama, who attended a fundraiser in New York on Thursday for Gay Pride Week, has a nuanced stance on gay issues. Experts say he could risk alienating large portions of the electorate if he came out strongly in favor of such matters as gay marriage before the 2012 elections.

During the 2008 election Obama picked up important support from Evangelicals, Catholics, Latinos and African-Americans, some of whom oppose gay marriage, which has become a contentious social issue being fought state-by-state.

In California a judge last year overturned a ban on gay marriage, but no weddings can take place while the decision is being appealed. It could set national policy if the case reaches the U.S. Supreme Court.

Connecticut, Iowa, Massachusetts, New Hampshire, Vermont and the District of Columbia allow same-sex marriage, and Delaware, Hawaii, Illinois and New Jersey approved civil unions. The first legal same-sex marriages in the United States took place in Massachusetts in 2004.

But gay marriage is banned in 39 states.

In New York a recent Siena poll found 58 percent of New Yorkers support gay marriage, while nationally the U.S. public is nearly evenly split, with 45 percent in favor and 46 percent opposed, according to a Pew Research poll released last month.

New York's Democrat-dominated Assembly voted 80-63 in favor of gay marriage last week and passed the amended legislation on Friday 82-47.

A key sticking point had been over an exemption that would allow religious officials to refuse to perform services or lend space for same-sex weddings. Most Republicans were concerned the legal protection was not strong enough, so legislative leaders worked with Cuomo to amend his original bill.

"God, not Albany, settled the definition of marriage a long time ago," said Senator Ruben Diaz Sr., a Pentecostal minister and the only Democrat to vote against the measure.

However, fears of a slew of litigation arising from a possible religious exemption to New York's proposed same-sex marriage law are not borne out by experience with similar laws in other states, legal experts say.