Victory takes Barca top of the table thanks to their better head-to-head record with both sides locked on 75 points.
Messi had broken a three-year goalless drought against Madrid to cancel out Casemiro's opener for Madrid in the first-half.
Ivan Rakitic's stunning strike and a red card for Madrid captain Sergio Ramos for chopping down Messi 13 minutes from time put Barca on course for victory before James Rodriguez came off the bench to level four minutes from time.
However, fittingly, Messi had the final say with a brilliant finish deep into stoppage time to lift Barcelona's hopes of a third straight La Liga title.
Victory takes Barca top of the table thanks to their better head-to-head record with both sides locked on 75 points, but Madrid have a game in hand.
"He is the best player for me in history and I have seen a lot of football," said Barca boss Luis Enrique as he celebrated victory in his final Clasico in charge.
"In modern football when everyone is tactically and physically better he still makes the difference.
"The fact he has scored his 500th goal shows all of us as Barca fans are lucky to have him identified with the club."
However, Madrid coach Zinedine Zidane lamented his side's inability to see the game out with a man less at 2-2 that would have seen them remain very much in the driving seat for a first title in five years.
"At 2-2 we should have used our heads more," said the Frenchman.
"The way we are, we are always positive and think we can score the third goal, but then we are all out of position and you see what happened."
Fresh from dumping Bayern Munich out the Champions League in a 4-2 thriller after extra-time in midweek, Madrid had started confidently in another breathless encounter at the Bernabeu.
Cristiano Ronaldo had strong appeals for a penalty waived away inside two minutes after he appeared to have been clipped by Samuel Umtiti.
Shortly after Messi was left floored and bloodied after being caught by a stray elbow from Marcelo.
The Argentine was still down by Ronaldo drilled another fine effort towards goal that Marc-Andre ter Stegen did well to parry to safety.
Unsurprisingly given both Madrid's potency and Barca's inability to defend set-pieces, the opener arrived from a dead ball.
Marcelo picked up the loose ball from a corner and when Sergio Ramos volleyed his cross against the post, Casemiro was on hand to tap into an unguarded net.
The lead lasted just five minutes, though, as with a handkerchief still mopping the blood from his cheek, Messi danced past Dani Carvajal and Nacho to fire home his first goal against Madrid for three years.
Gareth Bale's return from injury was a short-lived one as he was replaced by Marco Asensio before half-time.
However, Madrid were rampant at the start of the second period, but were met with a wall of resistence in Ter Stegen.
The German dived low to his left to turn international teammate Toni Kroos's effort behind before diverting Karim Benzema's goalbound header to safety with his outstretched foot.
However, Keylor Navas was also busy at the other end as he came to Madrid's rescue on three occasions.
Firstly, he got a foot to Paco Alcacer's prodded effort before pulling off acrobatic saves to deny Gerard Pique and Luis Suarez.
Ronaldo then missed a guilt-edged chance to put Madrid back in front as he somehow turned the ball over from point-blank range after unselfish play from Asensio.
There was nothing Navas could do to deny Barca 17 minutes from time when Rakitic took aim from 25 yards and drilled into the top corner.
The match seemed to have swung decisively in Barca's favour four minutes later when Ramos scythed down Messi to be shown the 22nd red card of his career.
However, even with 10 men, Madrid rallied and levelled when substitute James Rodriguez swept home Marcelo's cross four minutes from time.
There was still time for one huge twist in the title race, though, as Sergi Roberto's lung-bursting run led a Barca counter-attack to feed Jordi Alba and his cross was smashed into the corner by Messi.