Four points behind before kick-off, Carlo Ancelotti’s men breathed life into the title race by taking the lead at Espanyol through Cristiano Ronaldo while Barca were still struggling to break through at the Vicente Calderon.
Messi then scored to render Real’s result irrelevant, and Los Blancos had to restore their lead through a Marcelo strike and two more Ronaldo goals after Christian Stuani managed to level for Espanyol.
It finished 4-1 at El Prat, but Real’s challenge was over with one match of the season remaining.
Atletico’s defeat means they can be removed from third place by Valencia on the last day, given Los Che are only three points behind and trail Los Rojiblancos by a single strike in the goal-difference charts.
But a 1-1 draw with Celta Vigo, which saw Nicolas Otamendi rescue a point after Pablo Hernandez’s early opener, leaves Valencia vulnerable to Sevilla, who are one point shy in fifth following a 2-1 victory over Almeria.
Vicente Iborra scored twice for the Andalusians to cancel out Thievy’s first-half strike.
Athletic Bilbao mounted a late comeback to edge Elche 3-2 and move into seventh place.
The Alicante club led 2-0 thanks to a brace from Jonathas but Bilbao rallied to victory with Ager Aketxe, Mikel San Jose and Inaki Williams hitting three goals in the last 10 minutes.
Above them are Villarreal, 2-1 winners over Malaga, the team below Bilbao. The goals all arrived in the final eight minutes with substitute Gerard hitting two for the Yellow Submarine before Sergi Darder responded on 90 minutes.
The battle to avoid relegation will go to the final day with Eibar and Almeria occupying 18th and 19th spots while Deportivo La Coruna and Granada are two points better off and just clear of the drop zone after important wins.
Eibar could only draw 1-1 at Getafe as Borja’s goal cancelled out an effort from Fredy Hinestroza and Deportivo sit in a precarious position despite beating Levante 2-0 through strikes from Alberto Lopo and Juanfran.
Granada defied the odds to move into 16th as they hit David Moyes’ Real Sociedad for a 3-0 win at Anoeta.
The game was cruising towards a goalless draw when Granada mounted a late siege, with Youssef El Arabi, Robert Ibanez and Ruben Rochina plundering three goals in just 13 minutes.
Elsewhere, Rayo Vallecano moved into the top half by defeating basement boys Cordoba 2-1. Luis Eduardo Luso’s score for Cordoba arrived in between goals from Raul Baena and Adri Embarba.