“I had hesitated on gay marriage in part because I thought that civil unions would be sufficient,” Mr. Obama said. “I was sensitive to the fact that for a lot of people, the word marriage was something that invokes very powerful traditions and religious beliefs.”

Mr. Obama also invoked his Christian faith in explaining his decision.

“The thing at root that we think about is, not only Christ sacrificing himself on our behalf, but it’s also the golden rule — you know, treat others the way you would want to be treated,” he said. “And I think that’s what we try to impart to our kids, and that’s what motivates me as president.”

Reaction to Mr. Obama’s announcement was largely predictable — including immediate opposition from his presumptive Republican rival, Mitt Romney — yet people on both sides of the issue pointed to the historical significance of a president endorsing marriage between people of the same sex. It was a Democrat, Bill Clinton, who signed the Defense of Marriage Act, defining marriage as between a man and a woman, which the Obama administration last year decided not to enforce in the courts.

While Mr. Obama’s announcement was significant from a symbolic standpoint, more important as a practical matter were Mr. Obama’s decision not to enforce the marriage act and his successful push in 2010 to repeal the “don’t ask, don’t tell” law that prohibited openly gay men and lesbians from serving in the military. For that reason, gay rights groups had been largely enthusiastic about his re-election campaign while being pragmatically resigned to his not publicly supporting same-sex marriage before the election.

Mr. Obama’s announcement has little substantive impact — as an aide said, “It’s not like we’re trying to pass legislation.”

But the political impact is a wild card, even Obama advisers acknowledged, and it came one day after voters in North Carolina — the site of the Democratic Party’s nominating convention — supported a ban on same-sex marriage. But while the president has now injected a volatile social issue into the campaign debate, both sides say the election still is all but certain to turn on the economy.