Karim Benzema and Angel Di Maria bossed the first half, but both found themselves substituted in the second as Madrid moved into crisis management.
Andres Iniesta put Barca ahead just four minutes in, but Benzema's brace turned it around before Messi levelled before the break.
Cristiano Ronaldo's penalty restored Madrid's lead but after Sergio Ramos brought down Neymar in the area, Madrid were down to 10 and two Messi penalties won it for the visitors.
The result meant Atletico Madrid ended the weekend at the top of the table after a 2-0 win away to Real Betis.
They made hard work of it against a side bottom of the table, but capitalised on the second-half dismissal of Betis forward Braian Rodriguez to score twice in seven minutes.
Rodriguez saw red in the 55th minute for his second bookable offence, and three minutes later Gabi's driven shot put Betis ahead before Diego Costa doubled the lead in the 64th minute.
Atletico are level on points with their neighbours with Barca lurking just one point behind.
Sevilla built on their dramatic Europa League triumph over Betis in mid-week by comfortably beating Osasuna 2-1 away from home.
Jairo Samperio opened the scoring midway through the first half with a fine individual goal, and Carlos Bacca doubled the lead from the penalty spot just before half-time in a highly controversial incident, which saw Osasuna coach Javi Gracia sent away from his dugout for protesting the decision.
Javier Acuna scored a fine goal for Osasuna in injury time, but it only served as a consolation for the Navarrans.
The victory sees Sevilla move on to 47 points and cement their bid to qualify for Europe again, while Osasuna, on 29 points, remain in danger of relegation.
Javi Fuego netted a first-half brace as Valencia got back to winning ways in the Primera Division with a 2-1 victory over Villarreal at the Mestalla.
Juan Antonio Pizzi's side, who cruised into the quarter-finals of the Europa League with a 4-0 aggregate win over Ludogorets on Thursday, ended a three-match winless streak in the league on Sunday, moving up two places into eighth as a result.
The defeat saw derby rivals Villarreal, who scored a late consolation through Giovani dos Santos, move down a place into seventh in the standings.
Real Madrid: Benzema 20, 24, Ronaldo pen 55
Barcelona: Iniesta 7, Messi 42, pen 65, pen 84
COMMENT RULES: Comments that are judged to be defamatory, abusive or in bad taste are not acceptable and contributors who consistently fall below certain criteria will be permanently blacklisted. The moderator will not enter into debate with individual contributors and the moderator’s decision is final. It is Belfast Telegraph policy to close comments on court cases, tribunals and active legal investigations. We may also close comments on articles which are being targeted for abuse. Problems with commenting? firstname.lastname@example.org