The court’s 5-4 vote said the Defense of Marriage Act, known as Doma, denied equal protection to same-sex couples.
The court also declined to rule on a California ban on same-sex marriage known as Proposition 8. The decision paves the way for gay unions there.
Opinion polls show that most Americans support gay marriage.
Wednesday’s decisions do not affect the bans on same-sex marriage enshrined in the constitutions of more than 30 US states.
Twelve US states and the District of Columbia currently recognise gay marriage.
‘Doma writes inequality’
The Doma decision means that legally married gay men and women are entitled to claim the same federal benefits available to opposite-sex married couples.
On Wednesday morning, crowds gathered outside the Supreme Court hours before the rulings were due, in the hope of getting a seat inside the courtroom.
The legal challenge to Doma was brought by New York resident Edith Windsor, 83.
She was handed a tax bill of $363,000 (£236,000) when she inherited the estate of her spouse Thea Speyer – a levy she would not have had to pay if she had been married to a man.
“Doma writes inequality into the entire United States Code,” Justice Anthony Kennedy wrote in Wednesday’s ruling.
“Under Doma, same-sex married couples have their lives burdened, by reason of government decree, in visible and public ways,” the decision added.
“Doma’s principal effect is to identify a subset of state-sanctioned marriages and make them unequal.”
Lower courts also ruled in Ms Windsor’s favour, saying that Doma did not treat all married couples equally.
In the wake of the ruling on Doma, US President Barack Obama, who is on a state visit to Senegal, tweeted: “Today’s DOMA ruling is a historic step forward for #MarriageEquality #LoveIsLove”
The challenge to Doma did not address the question of whether same-sex marriage is constitutional.
Proposition 8 is a ban on gay marriage passed by California voters in November 2008, just months after the state’s supreme court decided gay marriage was legal.
Two same-sex couples then launched a legal challenge against Proposition 8. But as the state of California refused to defend it, the group that sponsored the measure stepped up to do so.
On Wednesday, the US Supreme Court said a private party did not have the right, or “standing”, to defend the constitutionality of a law.
“We have no authority to decide this case on the merits,” Chief Justice John Roberts wrote in the opinion, which was also decided by a margin of 5-4, though not along ideological lines.
The court also said the party defending the ban could not demonstrate that they would suffer injury if the law were to be struck down.
The four dissenting justices said they believed the court should have addressed the constitutional question of same-sex marriage before them in the Proposition 8 case.
Further litigation could lie ahead for the California ban, analysts say.
About 18,000 same-sex couples were married in California in the less than five months that same-sex marriages were permitted there.
Critics of the measure said Proposition 8 was unconstitutional because it took away previously granted rights from gay and lesbian couples.
Doma was signed into law in 1996 by former President Bill Clinton after it was approved in Congress with bipartisan support.
But it was subsequently struck down by several lower courts.
In 2011, President Barack Obama said that while he would continue to enforce Doma, he ordered his administration not to defend it in court. So Republicans from the House of Representatives argued in favour of the measure.
Last year, President Obama became the first sitting president to publicly endorse same-sex marriage.