Andres Iniesta put Barca ahead just seven minutes in, but Karim Benzema's brace turned it around before Messi levelled before the break. Cristiano Ronaldo's penalty restored Madrid's lead but after Sergio Ramos brought down Neymar in the area, Madrid were down to 10 and two Messi penalties won it for the visitors.
The game started at a frantic pace and rarely eased up.
Neymar and Benzema had already had attempts on goal before Barca took the lead, with Messi playing a perfect pass into the path of Iniesta who rifled a left-footed shot into the roof of the net.
Madrid had no need to panic, with Angel Di Maria setting up a string of chances.
Benzema was guilty of wasting two of them, hitting a tame shot from the edge of the area and then, after being picked out by Di Maria six yards out, inexplicably blazing over.
Neymar was almost through on goal on 19 but he was off-balance before Marcelo got in a block, and Madrid raced down the other end to score - Di Maria the inevitable provider as Benzema powered in a header which Victor Valdes got a hand to but could only help on its way.
As Madrid's celebrations broke up, Di Maria crumpled to the ground appearing to have been taken ill. Doctors quickly surrounded him as he spent a couple of minutes off the pitch, but whatever they gave him did the trick as he ran back on to set up Benzema's second moments later.
Running on to a quick pass from Marcelo, Di Maria stood up a cross and, after Javier Mascherano missed the interception, Benzema took a touch before rifling the ball past the helpless Valdes.
Gerard Pique's goalline clearance was all that denied Benzema a hat-trick after yet another brilliant run from Di Maria as the Argentinian continued to cause all manner of problems.
He next won a free-kick which led to penalty appeals as Madrid claimed handball from Cesc Fabregas in blocking Ronaldo's shot, but Messi and Barcelona had the perfect response.
Messi raced into the Madrid box and after playing a quick one-two fired a low shot in between three defenders to level it three minutes before half-time, his 234th La Liga goal putting him level with Hugh Sanchez for second all-time.
As Barca celebrated Pepe and Fabregas clashed, earning a booking each.
It was Madrid who had their tails up as the second half started, and this time it was Bale who made his first significant run before teeing up Benzema only for Valdes to make a fine save.
Moments later came a moment of controversy as Ronaldo was clipped by Dani Alves to earn a penalty, only for replays to show the contact was outside the area before Ronaldo's exaggerated tumble.
The referee may have made a mistake but Ronaldo did not as he fired in a low penalty.
Ten minutes later, Barca had one of their own as Neymar ran into the area and was clipped by Sergio Ramos. Contact was minimal, but Ramos was the one now given his marching orders. It was the 13th red card in the last 14 Clasicos, and the 19th of Ramos' career.
Messi fired in the spot-kick to level the match and Carlo Ancelotti made a huge call in sacrificing Benzema for Raphael Varane.
Alves gave Madrid a 74th-minute alarm call with a 20-yard drive that crashed back off the left-hand upright.
Then Barca were awarded their second penalty in the 83rd minute, the referee immediately blowing after Iniesta tried to wriggle between Daniel Carvajal and Xabi Alonso and fell.
Madrid's players angrily surrounded the referee but Messi kept his cool to fire an unstoppable penalty into the top right-hand corner.
Di Maria, sensational in the first half but subdued in the second, was taken off in favour of Isco but Madrid could not deny Barca a win which leaves them one point behind pacesetters Atletico and Real.