The government agreed with Phillips that his cakes are a form of expression, and he cannot be compelled to use his talents for something in which he does not believe.
"Forcing Phillips to create expression for and participate in a ceremony that violates his sincerely held religious beliefs invades his First Amendment rights," Acting Solicitor General Jeffrey B. Wall wrote in the brief.
The DOJ's decision to support Phillips is the latest in a series of steps the Trump administration has taken to rescind Obama administration positions favorable to gay rights and to advance new policies on the issue.
But Louise Melling, the deputy legal counsel of the American Civil Liberties Union, which is representing the couple, said she was taken aback by the filing.
"Even in an administration that has already made its hostility" toward the gay community clear, Melling said, "I find this nothing short of shocking."
Since taking office, President Trump has moved to block transgender Americans from serving in the military and his Department of Education has done away with guidance to schools on how they should accommodate transgender students.
The DOJ also has taken the stance that gay workers are not entitled to job protections under federal anti-discrimination laws. Since 2015, the Equal Employment and Opportunity Commission has taken the opposite stance, saying Title VII, the civil-rights statute that covers workers, protects against bias based on sexual orientation.
Federal courts are split on that issue, and the Supreme Court this term might take up the issue.
Indeed, lawyers for Jameka Evans, who claims she was fired by Georgia Regional Hospital because of her sexual orientation and "nonconformity with gender norms of appearance and demeanor," on Thursday asked justices to take her case.
Citing a 1979 precedent, a panel of the U.S. Court of Appeals for the 11th Circuit rejected her protection claims.
Taking that case, along with Masterpiece Cakeshop v. Colorado Civil Rights Commission, would make the coming Supreme Court term the most important for gay rights issues since the justices voted 5 to 4 in 2015 to find a constitutional right for same-sex couples to marry.
The case of Phillips, a baker in the Denver suburbs, is similar to lawsuits brought elsewhere involving florists, calligraphers and others who say providing services to same-sex weddings would violate their religious beliefs. But these objectors have found little success in the courts, which have ruled that businesses serving the public must comply with state anti-discrimination laws.
Mullins and Craig visited Masterpiece Cakeshop in July 2012, along with Craig's mother, to order a cake for their upcoming wedding reception. Mullins and Craig planned to marry in Massachusetts, where same-sex marriages were legal at the time, and then hold a reception in Colorado.
But Phillips refused to discuss the issue, saying his religious beliefs would not allow him to have anything to do with same-sex marriage. He said other bakeries would accommodate them.
The civil rights commission and a Colorado court rejected Phillips' argument that forcing him to create a cake violated his First Amendment rights of freedom of expression and exercise of religion.
The court said the baker "does not convey a message supporting same-sex marriages merely by abiding by the law."