The General Court of the European Union, the bloc's second-highest tribunal, said the European Commission had "erred in its assessment of the facts" when it ruled that the tax arrangements amounted to illegal state aid to the clubs.
Tuesday's ruling means the clubs do not have to repay millions of euros to the Spanish authorities, as they had been ordered to do in the 2016 decision.
After a two-and-a-half year probe, the commission, which acts as the EU's anti-trust enforcer, ruled that tax arrangements Spain gave four clubs -- Real, Barca, Athletic Bilbao and Osasuna -- breached the bloc's rules on state aid.
But after a challenge by Barcelona, the Luxembourg-based General Court said that the commission had "not shown to the requisite legal standard that the measure at issue conferred an advantage on its beneficiaries" and threw out the 2016 decision.
Under European Union anti-trust rules, governments are not allowed to provide state aid to commercial enterprises if it distorts market competition.
The commission ruling said the four clubs were treated as non-profit organisations, which pay a lower tax rate than other professional clubs registered as limited liability companies.