Chasing a fifth title in six years, defeat for FC Barcelona at Real Madrid's Bernabeu stadium would have all-but killed off their challenge and they came out swinging and took a seventh-minute lead when playmaker Andres Iniesta burst clear on the left and lashed the ball into the roof of the net.
It was fitting start to a game featuring some of the world's most expensive and best-paid talent and watched by millions around the world that also set the stage for one of the most entertaining encounters between the game's two richest clubs of recent times.
FC Real Madrid, looking to restore their three-point lead at the top and undefeated in 31 matches in all competitions, were reeling but hit back with a Karim Benzema double before Messi's first, a close-range snap shot through a crowd of players to make it 2-2 at the break.
Cristiano Ronaldo put FC Real Madrid ahead from the penalty spot 10 minutes into the second half after he was tripped by Dani Alves and the match turned when home captain Sergio Ramos was sent off for felling Neymar when he was through on goal in the 63rd.
Messi clipped home the resulting penalty to level at 3-3 and after some relentless FC Barcelona pressure struck the winner from the spot six minutes from time after Iniesta was knocked to the ground in the area.
The Real Madrid players were furious about both penalty decisions but FC Barcelona played a courageous game as they had promised and dominated possession, particularly after Ramos was dismissed, and thoroughly deserved the victory.
With nine games left, Real have 70 points, level with city neighbours Atletico Madrid, who won 2-0 at bottom side Real Betis in the earlier kickoff, and one ahead of third-placed FC Barcelona.
"It was an emotional match," FC Barcelona fullback Jordi Alba said in an interview with Spanish television broadcaster Canal Plus.
"We started strongly and then fell behind but we reacted well and managed to get an important win," added the Spain international.
"The games that remain are like finals for Barcelona, Atletico and Real Madrid. This victory will give us a big morale boost."
Ronaldo and Ramos were particularly harsh in their criticism of referee Alberto Undiano Mallenco, suggesting he should not be allowed to officiate another 'Clasico', but Real coach Carlo Ancelotti refused to get drawn into the debate.
He urged his players to put Sunday's reverse behind them and regroup for a potentially tricky encounter on Wednesday at fifth-placed Sevilla, who won 2-1 at Osasuna on Sunday.
"We made some small mistakes and that's why we did not win," Ancelotti, in his first season in charge after succeeding Jose Mourinho, told a news conference.
"We pressured them high up the pitch, we had chances and we are not happy but not to worry, the league is still open," the Italian added.
"We fought and played with intensity but we have to forget this match. Wednesday's game could be a key match in the season."
Messi's treble, his second in two games after his hat-trick against Osasuna last weekend, meant the four-times World Player of the Year became the all-time top scorer in 'Clasicos' with 21 goals, surpassing Real great Alfredo Di Stefano who scored 18.
Still only 26, Messi has also moved into second on the list of record scorers in La Liga on 236 goals, ahead of former Real striker Hugo Sanchez on 234, and he is fast closing in on Telmo Zarra's mark of 251.
In Seville, Atletico initially struggled to create chances against a dogged Betis side, who are bottom but are not giving up their top-flight status without a fight and seized the initiative when forward Braian Rodriguez received a second yellow card for a handball in the 56th minute.
Midfielder Gabi, who rarely scores but hit a long-range stunner in this month's 2-2 draw at home to Real, cracked the ball into the corner from distance two minutes later and Diego Costa added a second from Koke's neat headed layoff in the 64th, his 23rd La Liga goal of the campaign.
Atletico's fine season under coach Diego Simeone has left them in with a real chance of becoming the first team other than Real or FC Barcelona, the world's richest clubs by income, to win La Liga since Valencia 10 years ago.
If Atletico and FC Real Madrid finish the season level on points, Atletico will take the title as they have a better head-to-head record thanks to the 2-2 draw at the Calderon and a 1-0 win at the Bernabeu in September.
Likewise, FC Barcelona have a superior head-to-head over FC Real Madrid after they beat them 2-1 at the Nou Camp in October, the last time the Madrid club lost before Sunday.
Villarreal's hopes of securing a place in Europe suffered another blow when they lost 2-1 at regional rivals Valencia, their fourth consecutive game without a win.
Villarreal are seventh on 45 points, one behind Real Sociedad, who play at Almeria on Monday, and six ahead of Valencia.