The European Commission said Spanish government support, including tax breaks and overpriced land transfers, gave the clubs an “unfair advantage” over others.
The other clubs were Valencia and Athletic Bilbao in the Spanish top flight, and Atletico Osasuna, Elche and Hercules in the second division.
The decision followed a two-and-a-half year investigation.
“Using taxpayers’ money to finance professional football clubs can create unfair competition,” EU Competition Commissioner Margrethe Vestager said in a statement.
“Professional football is a commercial activity with significant money involved and public money must comply with fair competition rules.
“The subsidies we investigated in these cases did not.”
Under European Union anti-trust rules, governments are not allowed to provide state aid to commercial enterprises if it distorts market competition.
The EU ordered Spain to recover unpaid tax of up to €5 million (Dh20.5m) from each of Barcelona, Real, Athletic Bilbao and Atletico Osasuna.
The exact sums would be determined by Spanish authorities, it said.
It 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.
Real Madrid was meanwhile ordered to repay €18.4 million after the Commission found that the City of Madrid overvalued a land transfer from the club to the city by that amount.
Meanwhile the Commission said that Valencia, Hercules and Elche were given access to overly favourable loan terms when they were in financial difficulties.
Valencia has to repay €20.4 million, Hercules €6.1 million and Elche €3.7 million.
Separately the European Commission cleared support given to five Netherlands clubs – FC Den Bosch, MVV Maastricht, NEC Nijmegen, Willem II Tilburg, and PSV Eindhoven – saying it did not involve illegal state aid.
Follow us on Twitter @NatSportUAE
Like us on Facebook at facebook.com/TheNationalSport