MADISON, Wis. — A federal appeals court ruled Friday that a law giving clergy tax-free housing allowances is constitutional, overturning a ruling in Wisconsin that said it was an unconstitutional benefit and delivering a huge win to religious leaders who had fought to keep the substantial benefit.

The Wisconsin-based Freedom From Religion Foundation has been waging legal battles against the tax break for a decade. Several religious organizations intervened in the case, saying that the survival of many congregations depended on keeping the housing allowance. A group of more than 5,000 pastors signed on to an Alliance Defending Freedom letter supporting the exemption.

The 7th U.S. Circuit Court of Appeals said in its Friday ruling that the effect of the 1954 law is "neither to endorse nor to inhibit religion, and it does not cause excessive government entanglement." The court noted that Congress has been providing federal tax exemptions for religious organizations as far back as 1802.

Annie Laurie Gaylor, co-founder and co-president of the Freedom From Religion Foundation, said the group was weighing whether to ask the full 7th Circuit to review the case or take it to the U.S. Supreme Court.

"It's a blow," Gaylor said.

U.S. Department of Justice spokeswoman Kelly Laco called the ruling "a win for the religious protections enshrined" in the Constitution. The Justice Department was defending Treasury Department Secretary Steve Mnuchin, who was named in the lawsuit.

The Becket Fund for Religious Liberty intervened in the case on behalf of Embassy Church located on the south side of Chicago and several other congregations.

"This ruling is a victory not just for my church but for the needy south side Chicago community we serve — our youth, our single mothers, our homeless, our addicted, and our victims of gang violence," Embassy's pastor Chris Butler said in a statement. "I am grateful that I can continue serving them and living side by side with them to make our neighborhood a safer, more peaceful place."

Becket said in a statement that Butler's church can't afford to pay him a full salary, so the housing allowance makes it possible for him to afford to live near his church.

Under the federal law passed in 1954, a "minister of the gospel" doesn't pay income taxes on compensation that is designated part of a housing allowance. The Freedom From Religion Foundation, headquartered in Madison, argued that the law discriminates against secular employees.

The benefit saves clergy, including non-Christian religious leaders, about $800 million a year in taxes.

Under the law, housing allowances paid as part of clergy salary can be subtracted from their taxable income. The Freedom from Religion Foundation argued that a clergy member can use the untaxed income to purchase a home, and then, in a practice known as "double dipping," deduct interest paid on the mortgage and property taxes.

U.S. District Judge Barbara Crabb ruled in 2017 that the exemption provides an unconstitutional benefit to religious persons and no one else, violating the establishment clause of the U.S. Constitution.

The three appeals court judges who upheld the benefit as constitutional were William Bauer, Daniel Manion and Michael Brennan. Bauer was appointed by Republican President Gerald Ford. Manion was appointed by Republican President Ronald Reagan and Brennan was appointed by Republican President Donald Trump.