Under the law, those who act according to these beliefs in foster care, counseling, school administration, facility rentals and wedding services would have been fully shielded from a host of potential government actions involving such things as hiring or firing decisions, fees or the issuing of state contracts.

The law would have also allowed court clerks to refuse to grant wedding licenses to same-sex couples as long as accommodations were made for the applicants to receive their licenses anyway. Judge Reeves struck down that element in a separate ruling on Monday, saying that while state officials were free to disagree, the Supreme Court ruling on same-sex marriage is the law of the land.

In a footnote in his ruling on Thursday night — Judge Reeves, who was appointed by President Obama and is the second African-American federal judge in Mississippi — compared Mr. Bryant’s remarks on a state’s “right to self-governance” when it comes to gay marriage to former Gov. Ross Barnett’s 1962 speech before the Legislature in which he invoked states’ rights to oppose the integration of the University of Mississippi.

Mississippi’s law was condemned by civil rights activists, business groups and a number of the state’s mayors, particularly along the tourism-dependent Gulf Coast. But given its lack of Fortune 500 headquarters and its uncontested conservative political landscape, Mississippi did not face the same broad backlash as North Carolina did after passing a law restricting bathroom access for transgender people.

There have been multiple legal challenges with a wide array of plaintiffs: gay and straight, transgender and not. Lawyers in the case, which arose from suits filed by the Mississippi Center for Justice and the Campaign for Southern Equality, argued that the law was motivated by animus toward gay and transgender people and that it unconstitutionally endorsed and provided exclusive protection for “certain narrow religious beliefs.”

Lawyers for the state responded that the law did not affect the rights of gay or transgender people and that it would not favor a particular religious doctrine any more than conscientious objector laws do.

“Protection of free conscience and the free exercise of religion are legitimate and compelling governmental interests,” lawyers for the state said in a motion. It is reasonable to protect the convictions outlined in the law, they continued, “even though plaintiffs disagree with those beliefs and find them ‘offensive.’ ”