WASHINGTON — The Supreme Court overturned a nearly two-decade-old federal law defining marriage as a union between one man and one woman, a landmark decision on Wednesday that qualifies gay couples for federal benefits and underscores a rapid shift in American acceptance.

In a parallel case, the court cleared the way for same-sex marriage in California but dashed the hopes of gay marriage supporters that it would establish the constitutional right for gays and lesbians to marry in all states.

The pair of decisions set off joyous celebrations among gay rights supporters, who hailed the rulings as monumental changes that will dramatically improve their lives. At the same time, legal analysts said the narrowly written opinions left many future decisions about gay marriage to the states, where new challenges may occur.

The ruling that struck down a key portion of the 1996 Defense of Marriage Act will impact same-sex couples only in the District of Columbia and the 12 states in which gay marriage is recognized, including Massachusetts, which led the way in 2003.

The 5-to-4 ruling deemed the federal law unconstitutional under the equal protection clause. The power to regulate marriage falls to the states, not the federal government, wrote Justice Anthony Kennedy, the critical swing vote. He was joined by the four liberal justices: Ruth Bader Ginsburg, Stephen Breyer, Sonia Sotomayor, and Elena Kagan.

“The differentiation” between heterosexuals and gays “demeans the couple, whose moral and sexual choices the Constitution protects . . . and whose relationship the State has sought to dignify,” Kennedy wrote. “And it humiliates tens of thousands of children now being raised by same-sex couples. The law in question makes it even more difficult for the children to understand the integrity and closeness of their own family and its concord with other families in their community and in their daily lives.

“Under DOMA,” Kennedy continued, “same-sex married couples have their lives burdened, by reason of government decree, in visible and public ways. By its great reach, DOMA touches many aspects of married and family life, from the mundane to the profound.”

The law prevented married same-sex couples from obtaining government health care benefits, prohibited them from being buried together in veterans’ cemeteries, and forced them to follow complicated procedures to file tax returns jointly, Kennedy wrote. In all, the law denied married gay couples the right to more than a thousand federal benefits available to married heterosexual couples, including family medical leave and spousal Social Security benefits.

Scalia wrote that the court does not have the power to decide the case.

“By formally declaring anyone opposed to same-sex marriage an enemy of human decency, the majority arms well every challenger to a state law restricting marriage to its traditional definition,” Scalia wrote.

The DOMA case involved an 84-year-old widow, Edith Windsor, who was forced to pay more than $363,000 in federal estate taxes after the death of her female spouse because their marriage was not recognized under federal law. If Windsor had been married to a man, she would not have had to pay the estate tax.

The federal law was signed by President Clinton 17 years ago, when gay marriage was hypothetical. Since then, public and political opinion has shifted, with President Obama announcing his support for gay marriage rights last year.

Obama, who was aboard Air Force One on a trip to Africa when the court’s ruling was released, hailed the decision, saying in a statement, “We are a people who declared that we are all created equal – and the love we commit to one another must be equal as well.”

Just as the court’s landmark decision in Roe v. Wade did not end legal challenges to abortion, its decisions on gay marriage Wednesday are not likely to end the debate over whether gays and lesbians have the right to marry. Critics of gay marriage vowed to continue their fight.

Tony Perkins, president of the Family Research Council, a Washington-based organization focused on family and faith values, called the decision “absurd” for “asserting that Congress does not have the power to define the meaning of words in statutes Congress itself has enacted.”

The ruling that the federal government must recognize gay marriages in states that recognize them only raises more questions and throws open the doors for new rounds of litigation, Perkins said. It is not clear, for example, what would happen to married gay couples from Massachusetts if they were to move to Mississippi, one of 36 states that have banned gay marriage.

Kris Mineau, president of Massachusetts Family Institute, a Woburn-based organization that opposes gay marriage, said it is “preposterous” for the court to say the federal government has to recognize states’ definition of marriage.

“No way does it end the battles to define marriage from our side," Mineau said. “The vast majority of states maintain that marriage is a union between a man and a woman.”

Legal experts following the cases closely said that while striking down the federal law defining marriage is monumental, Kennedy’s carefully written opinion provides legal grounding for both critics and supporters of gay marriage in future challenges.

Kennedy emphasized states’ rights, yet struck the law on the grounds that it interferes with equal protection rights. He said the ruling would not necessarily commit the court to striking down state bans on gay marriage.

“On one hand you can read it as being about federalism, on the other hand it’s explicitly about equal protection. Once you start talking about equal dignity and respect, it’s not clear why that wouldn’t also apply at the state level,” said Michael Klarman, a Harvard law professor whose book “From the Closet to the Altar: Courts, Backlash, and the Struggle for Same-Sex Marriage” was published in October.

Attorneys supporting gay marriage cautioned against viewing the ruling as a panacea.

“It’s an enormous step forward but it’s just a first step forward,” said Ara Gershengorn, a partner with Boston-based Foley Hoag and coauthor of an amicus brief filed on behalf of former US Cabinet secretaries and other senior officials from various administrations.

Also, Wednesday’s ruling would not affect gay couples living in the eight states that have recognized civil unions or domestic partnerships.

“States have tried to treat same-sex couples the same as opposite-sex couples, but what this decision shows is that even if the states are trying to accomplish that, it still falls short of the goal,” Gershengorn said. “Civil unions still can’t accomplish what marriage does.”

In those states, “people will have to continue to work at the state level to get marriage,” said former US representative Barney Frank, a Massachusetts Democrat and the first openly gay congressman.

Frank said he is hopeful that the ruling will pave the way to overturning laws in some states that ban gay couples from adopting children.

The marriage ruling has immediate implications for one of the largest public institutions where federal benefits are paramount: the armed forces.

Though Congress lifted the ban on gays serving openly in the military in 2010, the Defense of Marriage Act has prevented the Pentagon from conferring on same-sex couples a host of allowances and compensation set aside for heterosexual couples and their dependents, including health care, education, and survivor benefits.

In the California case, the 5-to-4 ruling left in place a lower-court decision ending the California gay marriage ban, saying that those defending the ban did not have the right to step into the state’s role in appealing the trial court’s decision after the state chose not to do so.

‘‘We have no authority to decide this case on the merits,” Roberts wrote in the majority opinion. He was joined by Ginsburg, Breyer, Kagan, and Scalia.

Voters in California had banned gay marriage six months after the state Supreme Court endorsed such unions. The state’s constitution was amended to recognized marriages only between a man and a woman.

The plaintiffs in the California case rejoiced outside the courthouse after the rulings.

“Today we can go back to California and say to our own children, all four of our boys, that your family is just as good as anybody else’s family,” said Kristin Perry, standing next to her partner, Sandra Stier, in a press conference in front of the courthouse steps. “Now we will be married, and we will be equal.”

Bryan Bender and Matt Viser of the Globe staff contributed to this report. Tracy Jan can be reached at tjan@globe.com. Follow her on Twitter @GlobeTracyJan.

BostonGlobe.com complimentary digital access has been provided to you, without a subscription, for free starting today and ending in 14 days. After the free trial period, your free BostonGlobe.com digital access will stop immediately unless you sign up for BostonGlobe.com digital subscription. Current print and digital subscribers are not eligible for the free trial.

Thanks & Welcome to Globe.com

You now have unlimited access for the next two weeks.

BostonGlobe.com complimentary digital access has been provided to you, without a subscription, for free starting today and ending in 14 days. After the free trial period, your free BostonGlobe.com digital access will stop immediately unless you sign up for BostonGlobe.com digital subscription. Current print and digital subscribers are not eligible for the free trial.