WASHINGTON — In a narrow ruling, the U.S. Supreme Court gave a victory Monday to a Colorado baker who argued that he should not be compelled to make a cake for a same-sex wedding, in a case that pitted freedom of religion against marriage equality and protection from discrimination.

The 7-to-2 ruling was focused on this case, and did not address the larger question of whether the First Amendment protects providers of wedding and other services who have cited religious and free-speech objections when refusing to serve gay and lesbian customers in the wake of the Supreme Court's 2015 same-sex marriage decision.

At stake in the case, said the Conference of Catholics Bishops, was "the freedom to live according to one's religious beliefs in daily life." But civil rights groups warned that a win for the baker would create a religion-based exception to laws forbidding other forms of discrimination, too.

Jack Phillips of Masterpiece Cake in Denver said his cakes are works of art and requiring him to bake them for same-sex weddings would force him to express a view that violated his religious beliefs. He runs his business guided by religious principles, closing on Sunday and refusing to make cakes containing alcohol or celebrating Halloween.

"I don't want to be forced to create art, any of the things I do for an event, that goes against my faith," he said.

In 2012, when David Mullins and Charlie Craig walked into his shop and asked him to bake a cake for a reception to celebrate their wedding, Phillips said, "I'm sorry, guys, I can't do that."

What followed, Mullins said, "was this horrible pregnant pause while what was happening sunk in, and we were mortified."

After the couple filed a formal complaint, Colorado courts ruled that the state's public accommodation law, which bans discrimination by companies offering their services to the public, did not allow Phillips to refuse the gay couple's request. A reasonable person would assume that the cake expressed the message of the couple, not the baker, the courts said.

The ACLU, representing Mullins and Craig, said they never discussed with Phillips what kind of design, if any, they wanted on their cake, diminishing his claim that his freedom of expression was at stake.

"He simply turned us away just because of who we are instead of what we asked for," Craig said.

The ruling means that some business owners cannot be successfully sued for refusing on religious grounds to provide services to same-sex couples, even in the 22 states with human rights laws, similar to Colorado's, that ban discrimination on the basis of sexual orientation.

After Colorado's Civil Rights Commission said that Phillips could not discriminate by refusing to make cakes for same-sex weddings, he chose to stop baking wedding cases for all customers, which he said took away about 40 percent of his business.

He said a Supreme Court ruling in his favor would "declare to the world that my faith is not a scarlet letter."