The bad feeling between the two sides boiled over at the end of the match on Wednesday which saw three players sent off - Marcelo and Mesut Ozil of Real Madrid and Barcelona's David Villa.
Barcelona were far from their best with their players looking rusty and they lacked their usual slick passing, while Real took the game to them.
However, Andres Iniesta put Barca ahead with a deft finish before Cristiano Ronaldo scored his 100th goal in a Real Madrid shirt to equalise.
Messi, though, was the key player as he scored at the end of the first half and again three minutes from the end with French striker Karim Benzema finding the back of the net in between.
Barcelona coach Pep Guardiola was overcome with emotion.
"Words fail me in how to congratulate my players on how they played tonight," he said.
"They are extraordinary and legendary players."
Guardiola only gave new 29million pound signing former Arsenal captain Cesc Fabregas a few minutes of playing time towards the end of the game. But it was enough time for Marcelo to be dismissed for a late challenge on the Spanish midfielder.
Real continued to have more of the ball and Ronaldo was inches away from putting them ahead when his drive from the edge of the area was parried by Valdes onto the crossbar. Ozil then should have done better when his weak strike was comfortably blocked by the Barca keeper.
At the same time whenever Barca got the ball in attack they looked dangerous and Messi restored their lead a minute before the break. Pique back-heeled the ball into his path and Messi dinked the ball over Casillas.
Marcelo who came on for the second half appeared to kick out at Messi and there were angry exchanges between several of the players.
Messi, having only just returned to training after a belated break following the Copa America, was Barca's main danger as he forced Casillas into a low save.
Finally Fabregas was given his bow in front of the home fans with Pedro making way. Defender Sergio Ramos had a golden opportunity to head Real level before Benzema finally scrambled the ball home in the area.
The game looked to be heading for extra time when Messi pounced to fire home an Adriano cross.