In a surprise move, the Trump administration on Thursday sided with a Colorado bakery whose owner is arguing to the Supreme Court that he should not need to bake a cake for a same-sex couple when their ceremony violates his religious beliefs.

Colorado's public accommodation law bans discrimination based on sexual orientation, and its courts have ruled that businesses catering to the public cannot discriminate against same-sex couples seeking services for weddings or other commitment ceremonies.

Masterpiece Cakeshop, however, is run by Jack Phillips, a man who incorporates his Christian faith into the way he runs his business. Baking a cake, which he maintains is a form of creative expression, for a same-sex couple would violate his religious beliefs and, as such, he has refused to do so.

The Colorado Civil Rights Commission ruled against Phillips, as did Colorado courts, but the Supreme Court agreed this June to hear his case this fall.

On Thursday, in a filing at the high court, the Justice Department announced that it agrees with Masterpiece Cakeshop, arguing that it would create an "intrusion" on the First Amendment "where a public accommodations law compels someone to create expression for a particular person or entity and to participate, literally or figuratively, in a ceremony or other expressive event."

"Forcing Phillips to create expression for and participate in a ceremony that violates his sincerely held religious beliefs invades his First Amendment rights," the Justice Department lawyers, led by Acting Solicitor General Jeffrey Wall, wrote. Among other lawyers on the brief, Wall was joined by two other acting division heads: Chad Readler, leading the Civil Division, and John Gore, leading the Civil Rights Division.

In the brief, the Justice Department argues that "most applications of a public accommodations statute" are fine and do not raise the First Amendment concerns it discussed in Thursday's filing. However, reviewing the Supreme Court's decisions in cases involving a gay contingent seeking to participate in a group's St. Patrick's Day parade and a gay man who was rejected as a Boy Scouts leader, Justice Department lawyers argue there is an exception: "Heightened scrutiny [by courts] is appropriate at least where a law both compels the creation ... of speech or of a product or performance that is inherently communicative, and compels the creator’s participation in a ceremony or other expressive event."

One such circumstance, the brief goes on, is baking a wedding cake, and "Colorado cannot satisfy [heightened scrutiny] because it lacks a sufficient state interest to justify that intrusion on 'the core principle of speaker’s autonomy.'"

Specifically, the Justice Department argues, this is so because, while the Supreme Court has said that "'eradicating racial discrimination' in the private sphere is the most 'compelling' of interests," the high court "has not similarly held that classifications based on sexual orientation are subject to strict scrutiny."

The current case comes to the Supreme Court more than five years after the circumstances leading to the case took place.

In July 2012, Charlie Craig and David Mullins attempted to order a wedding cake from Masterpiece Cakeshop in Lakewood, Colorado, but owner Jack Phillips declined, saying that it would violate his religious beliefs.

While it remains legal in many parts of the US to turn gay couples away from businesses, 21 states ban discrimination in places of public accommodation on the basis of sexual orientation, including Colorado.

“I’ll make you birthday cakes, shower cakes, sell you cookies and brownies, I just don’t make cakes for same-sex weddings,” court records say Phillips told the men.

Represented by the ACLU, the couple filed a complaint with the Colorado Civil Rights Commission, which found in 2014 that the baker ran afoul of a state law banning discrimination on the basis of sexual orientation. A Colorado appeals court upheld that decision, saying that if the baker “wishes to operate as a public accommodation and conduct business within the State of Colorado, [the Colorado Anti-Discrimination Act] prohibits it from picking and choosing customers based on their sexual orientation.”

The court added that the law “does not impose burdens on religious conduct not imposed on secular conduct.”

Colorado's supreme court declined to take the case, which led to the request for the US Supreme Court to hear the case, which it agreed to do in June.

ACLU deputy director Louise Melling said in a statement on Thursday evening, “This brief was shocking, even for this administration. What the Trump Administration is advocating for is nothing short of a constitutional right to discriminate.”

A Justice Department official told BuzzFeed News the agency chose to file in the case "because the First Amendment protects the right of free expression for all Americans."

“Although public-accommodations laws serve important purposes, they — like other laws — must yield to the individual freedoms that the First Amendment guarantees,” the official continued in a statement. “That includes the freedom not to create expression for ceremonies that violate one’s religious beliefs.”