A divided Supreme Court on Monday absolved a Colorado baker of discrimination for refusing to create a custom wedding cake for a same-sex couple.

The verdict criticized the state's treatment of Jack Phillips' religious objections to gay marriage, ruling that a civil rights commission was hostile to him. As a result, the decision did not resolve whether other opponents of same-sex marriage, such as florists and photographers, can refuse commercial wedding services to gay couples.

Justice Anthony Kennedy wrote the court's 7-2 decision against the same-sex couple, Charlie Craig and Dave Mullins, departing from his long history of opinions in favor of gay rights dating back a generation. Included among them was the court's 2015 decision legalizing gay marriage nationwide.

"The outcome of cases like this in other circumstances must await further elaboration in the courts," Kennedy said. "These disputes must be resolved with tolerance, without undue disrespect to sincere religious beliefs, and without subjecting gay persons to indignities when they seek goods and services in an open market."

Justices Ruth Bader Ginsburg and Sonia Sotomayor cast the lone dissents. Fellow liberal Justices Stephen Breyer and Elena Kagan voted with the majority.

"Phillips would not sell to Craig and Mullins, for no reason other than their sexual orientation, a cake of the kind he regularly sold to others," Ginsburg said.

Kennedy reasoned that Phillips, in refusing to create a same-sex wedding cake, had good reason to believe he was within his rights. State law at the time allowed merchants some latitude to decline specific messages, such as those demeaning gay people and gay marriages.

The government cannot impose regulations hostile to citizens' religious beliefs, the ruling said.

During oral argument in December, Kennedy and other conservative justices had expressed concern about the potential effect on other merchants with strong religious objections to same-sex marriage, from chefs to florists.

The five-year-old legal battle between Phillips and customers Charlie Craig and David Mullins represented a test between the Constitution's guarantees of free speech and religion and laws in 22 states prohibiting discrimination against the LGBT community.

Phillips, 62, owner of Masterpiece Cakeshop, was fighting for the rights of "creative artists" to choose what they will sell. Craig, 37, and Mullins, 33, were fighting for the rights of LGBT customers to choose what they will buy.

Craig and Mullins won before the Colorado Civil Rights Commission and the state Court of Appeals, thanks to the state's inclusion of sexual orientation in its anti-discrimination law. Twenty-one other states have similar laws.

But the Supreme Court, bolstered last April by the addition of stalwart conservative and fellow Coloradan Neil Gorsuch, represented a tougher test.

The high court had weighed in twice before on the subject of same-sex marriage. In 2013, it ruled that the federal government must recognize gay and lesbian marriages in the 12 states that had legalized them. In 2015, it extended same-sex marriage nationwide.

But even as he authored the court's landmark decision, Kennedy held out an olive branch to religious conservatives.

"It must be emphasized that religions, and those who adhere to religious doctrines, may continue to advocate with utmost, sincere conviction that, by divine precepts, same-sex marriage should not be condoned," Kennedy wrote in 2015.