WASHINGTON -- U.S. President Barack Obama, who himself has acknowledged an evolution over gay rights, said Friday that the Supreme Court's ruling giving same-sex couples the right to marry nationwide represents a day when justice "arrives like a thunderbolt."

The president, in a White House statement, said that the court ruling has "made our union a little more perfect."

Obama didn't announce his own support for gay marriage until 2012 but had championed other aspects of gay rights, including a push to have Congress end the military's "don't ask, don't tell policy," which prohibited gay and lesbian people from serving openly in the military, in 2010. The policy officially ended in 2011.

For Obama, the ruling capped three days of upbeat, legacy building events, starting with congressional support for his trade agenda and Thursday's Supreme Court's decision to uphold a key piece of his landmark health care law.

But the day also offered Obama a jarring juxtaposition of emotions as he promptly left the White House to deliver a eulogy at the funeral for a pastor and state lawmaker who was killed in a historic black church together with eight parishioners by a white gunman last week.

In his remarks, Obama said the court's gay marriage ruling will end the patchwork of laws on marriage across the country and the uncertainty that they create for same-sex couples.

"This decision affirms what millions of Americans already believe in their hearts," he said. "When all Americans are treated as equal, we are all more free."

Immediately after the court ruling, Obama tweeted: "Today is a big step in our march toward equality. Gay and lesbian couples now have the right to marry, just like anyone else. #LoveWins." Likewise, first lady Michelle Obama tweeted: "This decision recognizes the fundamental truth that our love is all equal. Today is a great day for America. #LoveWins"

As staffers began to fill the White House landmark Rose Garden ahead of his remarks, Obama sat in his Oval Office in shirt sleeves and telephoned the lead plaintiff in the case, James Obergefell, who took the call on the plaza in front of the Supreme Court.

The president told Obergefell that his case was "going to bring about a lasting change in this country. It's pretty rare when that happens."

Meanwhile, the White House changed the profile avatar on its Twitter account to a rainbow-coloured image of the White House.