The Supreme Court seemed closely divided Tuesday over whether the First Amendment protects a Colorado baker from creating a wedding cake for a same-sex couple, with Justice Anthony M. Kennedy likely to cast the deciding vote.

Kennedy, who wrote the court’s 5 to 4 decision in 2015 saying gay couples have a constitutional right to marry, speculated about what might happen if a decision in baker Jack C. Phillips’s favor prompted requests for bakers across the country to refuse to make cakes for same-sex couples. Would the federal government feel vindicated? Kennedy asked.

On the flip side, just moments later, Kennedy sharply questioned Colorado Solicitor General Frederick R. Yarger. The justice seemed offended by a comment made during the deliberations of the Colorado Civil Rights Commission when one commissioner said: “And to me it is one of the most despicable pieces of rhetoric that people can use to — to use their religion to hurt others.”

At one point, Kennedy and some conservative justices raised the possibility that the proceedings against baker Jack C. Phillips had been infected by bias.

The rest of the court seemed to line up as expected. Liberal justices worried that an exception for Phillips would gut public accommodations laws that require businesses to serve the public without discriminating because of race, gender, religion and, in the case of Colorado and more than 20 other states, sexual orientation.

Justice Stephen G. Breyer said the court did not want to “undermine every single civil rights law.”

The court’s conservatives were concerned with what Justice Samuel A. Alito Jr. said was a “disturbing record” from the Colorado Civil Rights Commission and the Colorado Court of Appeals, which ruled against Phillips.

That raised the possibility that the case could be returned. But they also seemed sympathetic to Phillips’s argument that, as a “cake artist,” the law violates his freedom of expression to create a custom cake for a same-sex wedding. His religious beliefs teach that marriage is only between a man and a woman.

David D. Cole, legal director for the American Civil Liberties Union, which represents the couple, Charlie Craig and David Mullins, acknowledged there were complicated issues, but they did not apply to Phillips’s decision that he would not create a cake for the couple.

“All he knew was that they were gay,” Cole said.

Several of the liberal justices questioned what other types of business owners would be exempt if the court made an exception for Phillips.

“Who else is an artist?” asked Justice Ruth Bader Ginsburg.

What about a hair stylist, a chef or a makeup artist, asked Justice Elena Kagan.

Phillips’s attorney, Kristen K. Waggoner, distinguished between the baker’s highly-stylized, sculpted creations and the services provided by other professions that she said were “not speech.”

“Some people might say that about cakes,” responded Kagan.

The Trump administration filed a brief on behalf of Phillips; supporters of the couple said it was the first time the government has argued for an exemption to an anti-discrimination law.

But the government agreed with Phillips that his cakes are a form of expression and that he cannot be compelled to use his talents for something that he does not support.

U.S. Solicitor General Noel J. Francisco, representing the Trump administration, told the court Tuesday that the exemption should apply only to a narrow category of business owners who should not be forced to create or contribute to an event they disagree with on the basis of their religious beliefs.

He repeatedly used as an analogy an African American artist, who he said should not be compelled to sculpt a cross that would be used for a Ku Klux Klan service.

When asked where to draw the line, he said the justices should ask whether the creation is “predominantly expressive” in its purpose and whether customers are paying a premium for it.

That prompted a lighthearted retort from Justice Neil M. Gorsuch, who noted that high-priced wedding cakes rarely taste as good as they look.

It was to Francisco that Kennedy posed his question about whether the baker could post a sign in the window notifying potential customers that the shop does not make cakes for same-sex couples and asked whether that would be “an affront to the gay community.”

Phillips contends that dual guarantees in the First Amendment — for free speech and for the free exercise of religion — protect him against Colorado’s public accommodations law, which requires businesses to serve customers equally regardless of “disability, race, creed, color, sex, sexual orientation, marital status, national origin, or ancestry.”

Scattered across the country, florists, bakers, photographers and others have claimed that being forced to offer their wedding services to same-sex couples violates their rights. Courts have routinely turned down the business owners — as the Colorado Court of Appeals did in the Phillips, saying that state anti-discrimination laws require businesses that are open to the public to treat all potential customers equally.

There’s no dispute about what triggered the court case 1n 2012, when same-sex marriage was still prohibited in Colorado (Alito and Chief Justice John G. Roberts Jr. wondered whether that prohibition was significant). Craig and David Mullins decided to get married in Massachusetts, where it was legal. They would return to Denver for a reception, and those helping with the plans suggested they get a cake from Masterpiece.

The couple arrived with Craig’s mother and a book of ideas, but Phillips cut short the meeting as soon as he learned the cake was to celebrate the couple’s marriage.

Phillips recalled: “Our conversation was just about 20 seconds long. ‘Sorry guys, I don’t make cakes for same-sex weddings.’”

The couple then learned that Colorado’s public accommodations law specifically prohibited discrimination based on sexual orientation, and they filed a complaint with the Colorado Civil Rights Commission. The commission ruled against Phillips, and the appeals court upheld the decision.

“Masterpiece remains free to continue espousing its religious beliefs, including its opposition to same-sex marriage,” Judge Daniel M. Taubman wrote. “However, if it wishes to operate as a public accommodation and conduct business within the State of Colorado, [the law] prohibits it from picking and choosing customers based on their sexual orientation.”

The case is Masterpiece Cakeshop v. Colorado Civil Rights Commission.

Correction: A photo caption on an earlier version of this story gave an incorrect location for Masterpiece Cakeshop. The shop is in Lakewood, Colo.