Former President Bill Clinton, who signed the Defense of Marriage Act in 1996 barring federal recognition of same-sex weddings, called on the Supreme Court on Thursday to overturn the law.

Just weeks before the court takes up a case challenging the law, Mr. Clinton said he had come to believe that the law is unconstitutional and contravenes the quintessential American values of “freedom, equality and justice above all.” In doing so, he joined President Obama in arguing that the law be overturned.

“As the president who signed the act into law, I have come to believe that DOMA is contrary to those principles and, in fact, incompatible with our Constitution,” Mr. Clinton wrote in an op-ed article posted on the Web site of The Washington Post on Thursday evening.

The former president’s argument reflected a broader shift in societal attitudes in the 17 years since the law was enacted. Mr. Clinton was never enthusiastic about the measure, but he was not on record supporting same-sex marriage at the time and, just weeks before his re-election, he felt he had no choice but to sign it. Still, to make the point that he considered it politically motivated, and to call as little attention to it as possible, he signed it after midnight.

It was an awkward moment for Mr. Clinton, who had done more than any previous president to court the gay community and promote gay rights, but he believed that Republicans were trying to steer him out of what was then the mainstream and damage his chances for a second term. As more Americans have come to accept same-sex marriage, Mr. Clinton has spent the intervening years trying to explain and distance himself from the law. In 2011, he supported a measure in New York legalizing same-sex marriage.

The Defense of Marriage Act defined marriage for federal purposes as the union of a man and woman. It did not ban same-sex marriage in the states, none of which then had made it legal. But the law stipulated that should one or more states eventually authorize it, other states would not have to recognize the validity of such unions. The Supreme Court will hear oral arguments against the law on March 27.

In his op-ed piece, Mr. Clinton cast the decision to sign the law in 1996 as a sign of the era. “Although that was only 17 years ago, it was a very different time,” he wrote. “In no state in the union was same-sex marriage recognized, much less available as a legal right, but some were moving in that direction. Washington, as a result, was swirling with all manner of possible responses, some quite draconian.”

He suggested that the measure might have headed off a constitutional amendment barring same-sex marriage altogether and noted that only 81 of 535 members of Congress opposed the law. He pointed to a statement he issued when he signed it saying that the measure should not “be understood to provide an excuse for discrimination.”

“Reading those words today,” Mr. Clinton wrote, “I know now that, even worse than providing an excuse for discrimination, the law is itself discriminatory. It should be overturned.”