The Supreme Court’s first rulings on same-sex marriage produced historic gains for gay rights Wednesday: full federal recognition of legally married gay couples and an opening for such unions to resume in the nation’s most-populous state.

The divided court stopped short of a more sweeping ruling that the fundamental right to marry must be extended to gay couples no matter where they live.

But in striking down a key part of the 1996 Defense of Marriage Act (DOMA), the court declared that gay couples married in states where it is legal must receive the same federal health, tax, Social Security and other benefits that heterosexual couples receive.

In turning away a case involving California’s prohibition of same-sex marriage, known as Proposition 8, the justices left in place a lower court’s decision that the ban is unconstitutional. Gov. Jerry Brown (D) said he would order same-sex marriages to resume as quickly as possible.

The ruling means that same-sex marriage is now sanctioned in 13 states and the District of Columbia — a list representing more than a third of the population of the United States.

Justice Anthony M. Kennedy joined the court’s four liberals in declaring unconstitutional DOMA’s prohibition on federal recognition of legally married couples — enacted when such unions were only theoretical.

“DOMA writes inequality into the entire United States Code,” wrote Kennedy, who was joined by Justices Ruth Bader Ginsburg, Stephen G. Breyer, Sonia Sotomayor and Elena Kagan.

Withholding federal recognition of same-sex married couples places them “in an unstable position of being in a second-tier marriage,” Kennedy wrote. “The differentiation demeans the couple, whose moral and sexual choices the Constitution protects . . . and whose relationship the State has sought to dignify.”

Chief Justice John G. Roberts Jr. and Justices Antonin Scalia, Clarence Thomas and Samuel A. Alito Jr. dissented.

The decisions on the final day of the term set off a loud celebration in front of the court’s marble plaza and elsewhere in the country.

Edith Windsor, a New Yorker who brought the suit against DOMA after she had to pay an estate tax following the death of her wife, Thea Spyer, said she burst into tears upon hearing the court’s decision.

“If I had to survive Thea, what a glorious way to do it, and she would be so pleased,” Windsor said at a news conference.

President Obama, whose administration said it would not defend Section 3 of DOMA, because it believed the provision was unconstitutional, called Windsor and the challengers of Prop. 8 to congratulate them.

In a statement written on Air Force One en route to Africa, Obama said,“This ruling is a victory for couples who have long fought for equal treatment under the law; for children whose parents’ marriages will now be recognized, rightly, as legitimate; for families that, at long last, will get the respect and protection they deserve; and for friends and supporters who have wanted nothing more than to see their loved ones treated fairly and have worked hard to persuade their nation to change for the better.”

House Speaker John A. Boehner (R-Ohio) said the House Republican leadership defended DOMA “because the constitutionality of a law should be judged by the court, not by the president unilaterally.”

He said he was “obviously disappointed” by the decision, adding, “It is my hope that states will define marriage as the union between one man and one woman.”

Kennedy, who will turn 77 this summer, has authored the court’s foremost defenses of gay rights. Exactly 10 years ago Wednesday, he announced the court’s decision in Lawrence v. Texas, which struck down sodomy laws that targeted homosexuals.

His decision Wednesday striking down a central part of DOMA cited the principles of state autonomy, equal protection and liberty.

“The state’s power in defining the marital relation is of central relevance in this case,” not just because of federalism, Kennedy said, but because giving homosexuals the right to marry “conferred upon them a dignity and status of immense import.”

He said the history of the act showed that it was written to convey moral disapproval of homosexuality and “a stigma upon all who enter into same-sex marriages made lawful by the unquestioned authority of the states.”

In the end, Kennedy said, “DOMA is unconstitutional as a deprivation of the liberty of the person protected by the Fifth Amendment.”

Kennedy wrote that the opinion was applicable only in those states where same-sex marriage is legal.

In a withering dissent, Scalia said it took “real cheek” for the majority opinion to suggest such a limitation — because the rest of the ruling, he said, laid out a road map for how to challenge state bans on gay marriage.

“What has preceded that assurance is a lecture on how superior the majority’s moral judgment in favor of same-sex marriage is to the Congress’s hateful moral judgment against it,” Scalia wrote. “I promise you this: The only thing that will ‘confine’ the court’s holding is its sense of what it can get away with.”

He said that decisions about same-sex marriage should be decided in the political arena but that the majority took that away “to buy its stolen moment in the spotlight.”

Scalia also said the court’s ruling will raise practical problems: “Imagine a pair of women who marry in Albany and then move to Alabama, which does not ‘recognize as valid any marriage of parties of the same sex.’ . . . When the couple files their next federal tax return, may it be a joint one?”

Such a case does cause a problem for the Obama administration, which is now grappling with difficult questions about how to deliver federal benefits for same-sex couples living in states that do not have legal gay marriage.

Gay activists are pressuring Obama to use his executive authority to ensure that the full range of benefits, such as the right to file federal income taxes jointly or be exempt from the marriage estate tax, be granted to married same-sex couples no matter where they live.

DOMA was passed with bipartisan majorities of Congress and signed by President Bill Clinton — who put out a statement Thursday praising its demise.

Roberts wrote separately to say the majority had no evidence that Congress’s motivation was to “codify malice. . . . I would not tar the political branches with the brush of bigotry.”

Roberts also wanted to emphasize that neither of the court’s opinions Wednesday addressed the question of whether there is a broader right to marriage.

“We may in the future have to resolve challenges to state marriage definitions affecting same-sex couples,” Roberts wrote. “That issue, however, is not before us.”

In the Proposition 8 case, the court ruled 5 to 4 that those who appealed a decision throwing out California’s constitutional amendment banning same-sex marriage did not have legal standing to proceed. Thus, the Supreme Court did not rule on the merits of the case.

Only California officials may challenge a federal judge’s decision that Prop. 8 was unconstitutional, Roberts wrote for the majority, and they decided against it. The challenge at the Supreme Court was brought by those who favored Prop. 8, an initiative approved by the state’s voters.

“We have never before upheld the standing of a private party to defend the constitutionality of a state statute when state officials have chosen not to,” Roberts wrote. “We decline to do so here.”

The four dissenters — Kennedy, Thomas, Alito and Sotomayor — were justices who would probably disagree on the merits of the case but thought the court should hear them.

They said the ballot initiative process is worthless if state officials can simply refuse to enforce what the people have voted for.

Prop. 8 was passed by California voters in 2008 after the state’s top court ruled 5 to 4 that such unions must be allowed. About 18,000 couples were married after the decision and before Prop. 8 was approved, and those marriages remain valid.

U.S. District Judge Vaughn Walker ruled broadly in 2010 that the initiative was unconstitutional. A decision from the U.S. Court of Appeals for the 9th Circuit was more narrow, also overturning Prop. 8 but in a way that limited the impact to California.

The U.S. Supreme Court threw out that 9th Circuit decision, saying the appeal was not properly before it, and allowed Walker’s ruling to stand.

On Wednesday, it was almost difficult to remember how controversial it was when Theodore Olson and David Boies, adversaries at the Supreme Court in Bush v. Gore, teamed up to challenge the California constitutional amendment.

Even staunch supporters of same-sex marriage thought that bringing a lawsuit to the Supreme Court that argued for a constitutional right to marriage for gays would risk a setback that could take years to overcome.

But Jeff Zarrillo, who was part of one of the two gay couples named as plaintiffs in the case, said the legal challenge “changed the conversation. It altered the game. It created a groundswell of momentum and passion that brought us here to the Supreme Court today.”

The cases decided Wednesday were U.S. v. Windsor and Hollingsworth v. Perry.