The tax code has been very, very good to sports team owners.

A little-known tweak to IRS rules in 2004 has allowed new owners to write off nearly all of the purchase price of their teams against profits over 15 years.

The tax break has supercharged the increase in values of all professional sports teams, experts said.

For NFL team owners, the tax break has added roughly 5 percent to team values — or about $122 million, based on an average franchise value of $2.44 billion, according to Forbes.

The tax break, and others, and how each NFL owner has benefited from them came into focus this week after President Trump — as part of a long-running battle with the league and its players over kneeling during the national anthem — tweeted a threat about the league’s “massive tax breaks.”

“Change the tax law!” Trump tweeted.

While Trump was referring to tax breaks associated with the construction of NFL stadiums, the much more lucrative tax break is the ability of all sports team buyers to write down the value of the franchise over 15 years.

The tax law, long on the books, was significantly reshaped by President George W. Bush in 2004.

“Without a doubt, this is the tax break they benefit the most from,” tax expert Robert Willens told The Post.

Just how much?

Well, take the case of fracking billionaire Terry Pegula, who in 2014 paid $1.4 billion for the Buffalo Bills.

Pegula is able to deduct $93 million a year — one-fifteenth of the purchase price — against the team’s profits and his income for 15 years.

The Bills, according to Forbes, made $53 million last season. If Pegula earned $40 million in 2016, the Bills’ tax deduction could reduce his tax bill to zero.

Sports team owners who take the deduction have to pay it back when they sell the team — but that is likely far down the road, and may even be something children or grandchildren will deal with.

University of Michigan sports management Professor Rodney Fort wrote a report that estimates the depreciation tax break has caused baseball teams to rise 19 percent in value, he told The Post.

When the tax break was first introduced in 1946, it was aimed at players’ contracts, which, it was argued, were of diminishing value.

The IRS agreed, and courts have supported the position.

Today, with sports franchise values soaring, some think the tax break should be taken away.

“It’s absolutely fiction,” Fort said about the 2004 law. “There is no depreciation for which owners bear costs.”

The logic to getting rid of the law is straightforward and compelling, especially with the growing belief we should not be giving tax breaks to billionaires, Fort said.

“Maybe times have changed enough that changing the law might have more traction,” Fort said.

The NFL, in the middle of a scuffle with Trump, pointed out that no “special tax provision or advantage applies to the NFL alone.”

True.

But with the teams with the highest franchise values, the league has the most to lose.

The Bills did not return calls.