The court invalidated a provision of the federal Defense of Marriage Act that has prevented married gay couples from receiving a range of tax, health and retirement benefits that are generally available to married people. The vote was 5-4.
Justice Anthony Kennedy wrote the majority opinion.
Same-sex marriage has been adopted by 12 states and the District of Columbia. Another 18,000 couples were married in California during a brief period when same-sex unions were legal there.
The court has yet to release its decision on California's ban on same-sex marriage.
"Under DOMA, same-sex married couples have their lives burdened, by reason of government decree, in visible and public ways," Kennedy said.
"DOMA's principal effect is to identify a subset of state-sanctioned marriages and make them unequal," he said.
He was joined by the court's four liberal justices.
Chief Justice John Roberts and Justices Samuel Alito, Antonin Scalia and Clarence Thomas dissented.
Scalia read his dissent aloud. Scalia said the court should not have decided the case.
But, given that it did, he said, "we have no power under the Constitution to invalidate this democratically adopted legislation."
The law was passed in 1996 by broad majorities in the House of Representatives and the Senate, and signed into law by Democratic President Bill Clinton. Since then, many lawmakers who voted for the law and Clinton have renounced their support.