The announcement was the first by a sitting president, and Republican challenger Mitt Romney swiftly disagreed with it. "I believe that marriage is between a man and a woman," he said while campaigning in Oklahoma.
Gay rights advocates cheered Obama's declaration, which they had long urged him to make. Beyond the words, one man who married his gay partner in Washington, D.C., was stirred to send a $25 contribution to the president's campaign. "Making a contribution is the best way to say thank you," said Stuart Kopperman.
Obama revealed his decision after a series of events that made clear the political ground was shifting. He once opposed gay marriage but more recently had said his views were "evolving."
In an interview with ABC in which he blended the personal and the presidential, Obama said "it wouldn't dawn" on his daughters, Sasha and Malia, that some of their friends' parents would be treated differently than others. He said he also thought of aides "who are in incredibly committed monogamous same-sex relationships who are raising kids together."
Obama added that he 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 said he was taking a personal position. Aides said the president's shift would have no impact on current policies and he continues to believe that marriage is an issue best decided by states.
"I have hesitated on gay marriage in part because I thought that civil unions would be sufficient," Obama said in the interview. He added, "I was sensitive to the fact that for a lot of people the word `marriage' was something that evokes 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."
He spoke on the heels of a pair of events that underscored the sensitivity of an issue that has long divided the nation.
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.
Additionally, several of the president's biggest financial backers are gay, and some have prodded him publicly to declare his support for same-sex marriage.
Senior administration officials said Obama came to the conclusion that gay couples should have the right to legally marry earlier this year and had planned to make his views known publicly before the Democratic National Convention in early September. They conceded that Biden's comments accelerated the timeline, but said the vice president's remarks were impromptu and not part of a coordinated effort to soften the ground for a shift by the president. They spoke on condition of anonymity to discuss internal White House deliberations.
As recently as eight years ago, conservatives in several states maneuvered successfully to place questions relating to gay marriage on the election day ballot as a way of boosting turnout for President George W. Bush's re-election.
Now, nationwide polling suggests increasing acceptance of gay marriage. In a national survey released earlier this month, Gallup reported 50 percent of those polled said it should be legal, and 48 percent were opposed. Democrats favored by a margin of roughly 2-1, while Republicans opposed it by an even bigger margin. Among independents, 57 percent expressed support, and 40 percent were opposed.
Whatever the polls, the political crosscurrents are tricky, and administration officials conceded as much.
Some top aides argued that gay marriage is toxic at the ballot box in competitive states like North Carolina and said the vote there this week shows that opposition to the issue is a rallying point for Republicans.
Shifting his emphasis, even briefly, could open Obama up to Republican criticism that he is taking his eye off the economy, voters' No. 1 issue.
Yet some prominent gay donors have said publicly they wanted Obama to announce his support for gay marriage. Other Democratic supporters claim Obama's decision could energize huge swaths of the party, including young people. He also could appeal to independent voters.
By day's end Wednesday, the Obama campaign had emailed a clip of the interview and a personal statement from the president to its vast list of supporters, drawing attention to his stance.
The decision also creates an area of clear contrast between Obama and his Republican rival as he argues that he's delivered on the change he promised four years ago.
Obama 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."
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 a solicitation for campaign donations.
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 dad and a husband and hopefully the better I'll be as president," Obama said.
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.
Hear more about this tonight from the president on "World News with Diane Sawyer." The full interview will air on "Good Morning America" tomorrow.