President Obama announced Wednesday that he believes same-sex couples should be granted the right to marry, becoming the first U.S. president in history to fully embrace that level of civil rights for gays.

Obama’s announcement gave an immediate jolt to the decades-long movement for gay equality at a moment when a growing number of states are moving to ban — or legalize — same-sex unions and as polls show a majority of Americans support marriage rights.

For Obama personally, the disclosure completed a long conversion process that advisers said ended earlier this year after soul-searching and talks with his family.

“I’ve just concluded that for me personally it is important for me to go ahead and affirm that I think same-sex couples should be able to get married,” Obama said in an interview with Robin Roberts of ABC News.

Obama had hinted at that sentiment for years as he repeatedly said his views on gay marriage were “evolving,” but his statement still came as a dramatic election-year revelation that promised to energize advocates on both sides. Gay rights activists and many Democrats embraced the news, but some religious leaders, including one of Obama’s spiritual advisers, said they were distraught.

The potential dangers for Obama could be seen just one day before his announcement, when voters in North Carolina, a swing state critical to his reelection, voted overwhelmingly for a state constitutional amendment to ban same-sex unions. Obama hinted at the rapidly shifting political fault lines on Wednesday, saying he had weighed the teachings of his Christian faith against a growing pro-marriage consensus among younger Americans — a key target group for his reelection campaign.

“You know, when I go to college campuses, sometimes I talk to college Republicans who think that I have terrible policies on the economy, on foreign policy, but are very clear that when it comes to same-sex equality or, you know, sexual orientation, that they believe in equality,” Obama said in the interview, excerpts of which were aired during a rare midday newscast.

“They are much more comfortable with it,” Obama continued. “You know, Malia and Sasha, they have friends whose parents are same-sex couples. There have been times where Michelle and I have been sitting around the dinner table and we’re talking about their friends and their parents and Malia and Sasha, it wouldn’t dawn on them that somehow their friends’ parents would be treated differently. It doesn’t make sense to them and, frankly, that’s the kind of thing that prompts a change in perspective.”

Advocates put the announcement in historic terms, calling it an unprecedented moment of validation from the country’s most important political figure. Many said that Obama’s self-described evolution matched the thought process for non-gay Americans and that his comments could help push others to support marriage rights.

“President Obama made history by boldly stating that gay and lesbian Americans should be fully and equally part of the fabric of American society and that our families deserve nothing less than the equal respect and recognition that comes through marriage,” said Joe Solmonese, president of the Human Rights Campaign, a gay rights group close to the White House. The group’s incoming president, Chad Griffin, a major Obama fundraiser who has repeatedly pressed the president in personal conversations to support gay marriage, added that Obama’s words “will be celebrated by generations to come.”

But if the announcement thrilled an often disenchanted part of the president’s political base, it also came with obvious perils. Obama was immediately confronted with the potential backlash in a phone call to the Rev. Joel Hunter, an evangelical Florida pastor who acts as a spiritual adviser to the president and who said he told the president that he did not approve.

Hunter said that Obama called his cellphone as the pastor and his wife were driving in Orlando and that for 15 minutes the president explained his thinking.

“For him, there are competing moral values,” Hunter said in an interview. “One is the traditional view of marriage as seen in Scripture, and the other is fairness and equality.”

Hunter predicted the announcement would cause Obama “as much hurt as it will help” in the political arena. “There will be many groups who feel like it is an attack on the foundations of their faith,” Hunter said.

The announcement followed days of frenzied speculation after Vice President Biden unexpectedly told an interviewer that he was “absolutely comfortable” with same-sex marriage, a comment that focused attention on Obama’s 2010 statement that his views were still “evolving.”

Administration officials insisted that Obama had reached his own decision earlier this year but said that he told only a small circle of people, numbering roughly six or seven, and made clear to advisers that he wanted to announce his support before the Democratic National Convention in Charlotte, scheduled for the first week of September.

The timeline was sped up, however, by Biden’s comments, which aired Sunday on NBC’s “Meet the Press,” the officials said in a briefing with reporters Wednesday at the White House, speaking on the condition that they not be named or quoted.

White House officials learned about Biden’s statement soon after the news program was recorded Friday. They said Obama then decided he wanted to make his position known.

But first, there were several days of mixed messages. Obama campaign officials quickly called gay rights leaders to play down the vice president’s remarks. An internal debate ensued over how to handle the brewing storm, people familiar with the process said, and whether the president should speak out. Some political strategists worried about alienating conservative swing voters and some in Obama’s base, including African Americans, who tend to oppose gay marriage.

The officials said Obama had been influenced by the growing number of states engaged in a debate over the legality of same-sex marriage — whether in New Jersey, where a law was passed and vetoed, by Republican Gov. Chris Christie; in California, where an anti-gay-marriage measure passed in a 2008 referendum; or in New York state, where the legislature made it legal last year.

At a New York fundraiser in June, on the eve of the legislature’s action, Obama was interrupted by chants of “Marriage! Marriage!” as he listed his record on gay issues.

Obama asked himself what he would have done as a state legislator, administration officials said, and realized he would have voted in favor of legislation to legalize same-sex marriage.

If Obama had always intended to announce his views before the election, that was not clear to some of his closest gay allies, many of whom had been growing increasingly frustrated.

Obama has amassed a heavily pro-gay record, including the repeal of the military’s “don’t ask, don’t tell” policy. But advocates were angered last month when the White House announced that Obama had no plans to sign an executive order prohibiting discrimination by federal contractors on the basis of sexual orientation or gender identity — and his hedging on marriage was adding to the tensions. Both issues began to come up at LGBT fundraising events, and major donors were signaling they would decline to donate money to the super PAC supporting Obama.

Staff writers Ed O’Keefe, Philip Rucker and Felicia Sonmez contributed to this report.