The announcement was the first by a sitting president, and Obama's Republican rival, Mitt Romney, swiftly disagreed with it. "I believe that marriage is between a man and a woman," Romney said from Oklahoma.
Gay rights advocates cheered Obama's declaration, which they had long urged him to make. Joe Solomonese, president of the Human Rights Campaign, said it "extends that message of hope" to gays and lesbians.
Obama announced his shift -- he had said for years that his views on gay marriage were "evolving" -- in an interview with ABC in which he cited a blend of the personal and the presidential.
He said "it wouldn't dawn" on his daughters, Sasha and Malia, that some of their friends' parents would be treated differently than others, and added that he had thought of aides "who are in incredibly committed monogamous same-sex relationships who are raising kids together."
He added he had also thought about "those soldiers or airmen or Marines or sailors who are out there fighting on my behalf, and yet feel constrained even though now that `don't ask, don't tell' is gone because they're not able to commit themselves in a marriage."
The president's decision to address the issue came on the heels of a pair of events that underscored the sensitivity of the issue.
Vice President Joe Biden said in an interview on Sunday that he is completely comfortable with gays marrying, a pronouncement that instantly raised the profile of the issue. White House aides insisted the vice president hadn't said anything particularly newsworthy, but gay rights groups cited Biden's comments in urging the president to announce his support.
On Tuesday, voters in North Carolina -- a potential battleground in the fall election -- approved an amendment to the state constitution affirming that marriage may only be a union of a man and a woman.
While the nation appears roughly divided on the issue, the political cross-currents are tricky.
Some top aides argued that gay marriage is toxic at the ballot box in competitive states like North Carolina and Virginia because, as Tuesday's vote demonstrated, the issue remains a reliable way to fire up rank-and-file Republicans. It also could open Obama up to Republican criticism that he is taking his eye off the economy, voters' No. 1 issue.
Other Democratic supporters claim Obama's decision could energize huge swaths of the party, including young people. He also could appeal to independent voters, many of whom back gay marriage, and he could create an area of clear contrast between himself and his Republican rival as he argues that he's delivered on the change he promised four years ago.
Obama touched on that in the interview.
He said he sometimes talks with college Republicans on his visits to campuses, and while they oppose his policies on the economy and foreign policy, "when it comes to same sex equality, or, you know, sexual orientation, that they believe in equality. They are more comfortable with it."
On Tuesday, former Pennsylvania Gov. Ed Rendell, a Democrat, told Obama to "man up" and take a position on gay marriage. The president had already supported a number of initiatives backed by gays, including an end to "don't ask, don't tell," and decided not to defend in court a federal law that was designed as an alternative to gay marriage.
Maggie Gallagher, co-founder of the National Organization for Marriage and a leading supporter of the constitutional amendment approved in North Carolina on Tuesday, said she welcomed Obama's announcement at the same time she disagreed with it.
"Politically, we welcome this," she said. "We think it's a huge mistake. President Obama is choosing the money over the voters the day after 61 percent of North Carolinians in a key swing state demonstrated they oppose gay marriage."
House Democratic leader Nancy Pelosi instantly sought political gain from the president's announcement. The Democratic Congressional Campaign Committee issued an email in her name that asked recipients to "stand with President Obama." Such requests are often followed by future requests for campaign donations.
In the interview, Obama said, "I have hesitated on gay marriage in part because I thought that civil unions would be sufficient." He added, "I was sensitive to the fact that for a lot of people the word `marriage' was something that invokes very powerful traditions, religious beliefs and so forth."
Now, he said, "it is important for me personally to go ahead and affirm that same-sex couples should be able to get married."
Obama said first lady Michelle Obama also was involved in his decision and joins him in supporting gay marriage.
"In the end the values that I care most deeply about and she cares most deeply about is how we treat other people," he said.
Acknowledging that his support for same-sex marriage may rankle religious conservatives, Obama said he thinks about his faith in part through the prism of the Golden Rule -- treating others the way you would want to be treated.
"That's what we try to impart to our kids and that's what motivates me as president and I figure the most consistent I can be in being true to those precepts, the better I'll be as a as a dad and a husband and hopefully the better I'll be as president," Obama said.
Romney has not generally raised the issue in his campaign. He said earlier Wednesday that "I do not favor marriage between people of the same gender, and I do not favor civil unions if they are identical to marriage other than by name. My view is the domestic partnership benefits, hospital visitation rights, and the like are appropriate but that the others are not."
Public opinion on gay marriage has shifted in recent years, with most polls now finding the public evenly split, rather than opposed.
A Gallup poll released this week found 50 percent of all adults in favor of legal recognition of same-sex marriages, marking the second time that poll has found support for legal gay marriage at 50 percent or higher. Majorities of Democrats (65 percent) and independents (57 percent) supported such recognition, while most Republicans (74 percent) said same sex marriages should not be legal.
Six states -- all in the Northeast except Iowa -- and the District of Columbia allow same sex marriages. In addition, two other states have laws that are not yet in effect and may be subject to referendums.
AP Deputy Director of Polling Jennifer Agiesta and Philip Elliott in Colorado contributed to this report.