The Argentine became the tie's leading goalscorer after a cool finish and two penalties, while Sergio Ramos was sent off for the home side
Barcelona's all-time leading scorer struck two second-half spot kicks as the Catalan giants came from behind twice to win a pulsating encounter in the capital.
The second of those penalties, scored with six minutes left after Xabi Alonso clumsily fouled Andres Iniesta, was hammered into the top-right corner as the defending champions closed to within one point of Real - and city rivals Atletico - in the Liga table.
Iniesta's early goal was cancelled out by a pair of Karim Benzema strikes, and although Messi levelled before half-time, a controversial Cristiano Ronaldo penalty put Real back in front.
Sergio Ramos was then sent off for tripping Neymar, though, and Messi's first penalty restored parity.
And the Argentina star – who became the top scorer in Clasicos with his treble, overtaking Real Madrid legend Alfredo Di Stefano – held his nerve when presented with another penalty as his side notched their first league double over their rivals since the 2009-10 season.
Much was made of the influence Ronaldo, Gareth Bale and Neymar would have on the contest, but all three were not at their best, leaving Messi to play the starring role in the Spanish capital.
Neymar had an early strike saved before Iniesta fired Barcelona into the lead to cap a superb team move in the seventh minute.
Real Madrid supporters were quick to voice their displeasure as the visitors enjoyed a spell of possession, but their whistles turned to silence as Messi's angled ball sent Iniesta through on goal and the midfielder thrashed a powerful left-footed finish past Diego Lopez.
Benzema, who had already spurned two chances, missed a golden chance to level when he turned over after an Angel Di Maria cut-back, and Messi shot wide when he seemed more likely to double Barcelona's lead.
Benzema eventually scored for Real, with his powerful header – which came from another cross from the outstanding Di Maria - too hot to handle for Barcelona goalkeeper Victor Valdes, going in off the post to level proceedings in the 20th minute.
And Benzema doubled his account just four minutes later, as Di Maria, who needed treatment just moments before, again found the forward, who chested down before volleying a classic centre-forward's goal past Valdes.
Benzema could have had a hat-trick in the first 30 minutes, but saw Gerard Pique clear a curling effort off the line and Messi punished the hosts when he capitalised on a goalmouth scramble to score from close range just before the break.
Tempers frayed as Pepe and Cesc Fabregas were booked after the goal, but Real settled quickly after the interval, with Valdes doing well to deny Benzema after a great Bale run.
Real went ahead in controversial circumstances, as referee Alberto Undiano pointed to the penalty spot after Ronaldo was fouled by Dani Alves, although replays showed contact was initially made outside the area.
Ronaldo dispatched the spot-kick, powering an effort into the bottom left corner for his 26th league goal of the season, and Valdes had to keep out Bale after another Real attack.
The match swung in Barca's favour when an excellent Messi ball released Neymar, with the 19th red card of Ramos' Real Madrid career giving the Argentinian the chance to score from the penalty spot in the 65th minute.
And just like Ronaldo before him, Messi made no mistake, forcing Carlo Ancelotti to sacrifice Benzema for defender Raphael Varane.
A fizzing 20-yard effort from Alves then hit the inside of the left post with Lopez beaten, but Barcelona's numerical advantage was beginning to show.
Iniesta created the winner with a clever run, as his attempt to get away from Daniel Carvajal was blocked by Alonso, leaving Undiano with no option but to give another penalty.
And Messi ensured it was his name in the headlines again as he scored a crucial goal that is sure to ease the pressure on coach Gerardo Martino and put Barca firmy back in the title race.