Victory for Madrid would have all but sealed their first La Liga title in five years.
Instead, Barca are very much back in the hunt to win it for a third straight year under outgoing coach Luis Enrique.
Messi´s late intervention moves Barca top with both sides locked on 75 points.
However, Madrid still have the advantage as they have a game in hand.
On a pitch decorated with most of the biggest stars in world football, the diminutive Argentine still shone as the best player on the park by a distance.
As well as his late winner, Messi had brought Barca level with a stunning solo effort in the first-half.
The five-time World Player of the Year also lured Sergio Ramos into a wild challenge that saw the Madrid captain sent-off and Casemiro was also lucky to avoid a red card for two fouls on Messi before half-time.
Ter Stegen´s coming of age
That Barca were still in the game for Messi to make the telling impact, though, was thanks in large part to the brilliance of Marc-Andre ter Stegen in goal.
The German hasn´t always shone in his first season as Barca´s undoubted number one.
However, he is still just 24 and on this evidence can go onto become one of the best ´keepers in the world.
James can still make a big impact
James Rodriguez has endured a hugely frustrating two seasons since Zinedine Zidane took charge of Madrid.
The Colombian, who moved to Madrid for 80 million euros ($85.7 million) after launching himself as a global icon by top scoring at the 2014 World Cup, has fallen well down the pecking order at the Bernabeu.
However, in need of a goal, Zidane called on Rodriguez in the final stages and was rewarded just three minutes after his introduction with a wonderful finish that looked to have delivered Real a point at 2-2.
Ramos as reckless as ever
Ramos left the field pointing accusingly at Spanish international teammate and Barca defender Gerard Pique after being shown the 22nd red card of his career.
Pique has been outspoken in his belief that Madrid get more than their fair share of favourable refereeing calls.
However, Ramos could have few complaints as he lunged in two-footed on Messi to crucially leave his side a man down for the final 13 minutes.
It is the fifth time Ramos has been sent-off against Barca.