It was a pulsating encounter from start to finish, with Los Blancos taking the lead through Casemiro on 28 minutes, only for Messi to equalise five minutes later.
Ivan Rakitic put Luis Enrique's side ahead in the 73rd minute with a sweet strike and Sergio Ramos saw red four minutes later for a poor challenge on Messi.
James Rodriguez stepped off the bench to pull Real level with five minutes remaining, but Messi popped up at the death to score his 500th goal for Barca.
The hosts made a strong start and they wanted a penalty in the opening minute when Cristiano Ronaldo was caught on the shin by Samuel Umtiti, but the referee waved the appeals away.
Ronaldo then tested Marc-Andre ter Stegen five minutes later as he received a pass from Karim Benzema on the right and cut inside before hitting a low shot at the keeper from 20 yards out.
Barca had been starved of possession in the opening 10 minutes, but they carved out their first opportunity in the 11th minute when Jordi Alba found Luis Suarez just outside the box and the Uruguayan flashed a shot wide of the left post from 20 yards out.
Both teams had a chance to break the deadlock on 18 minutes, as Benzema fired tamely at Ter Stegen from inside the area after being played in on the right by Luka Modric, before Suarez sliced a shot well wide of the right post moments later from Paco Alcacer's lay-off.
Los Blancos went on to enjoy a good spell of pressure around the midway point of the first half, with Ronaldo cutting inside from the left flank and forcing a sharp save out of Ter Stegen with a shot from inside the area on the left in the 20th minute.
Two minutes later Gareth Bale attempted to take Toni Kroos' long ball out of the air after breaking through on goal, but the Welshman failed to make proper contact and Ter Stegen cleaned up, while Ronaldo curled an effort well wide on 24 minutes after being picked out by Marcello just ouside the box.
Real drew first blood four minutes later as Marcello clipped a cross to the back post, where Ramos fired against the upright and rebounded to Casemiro, who slotted home in off the left post.
But the Blaugrana responded swiftly as Messi collected the ball from Rakitic ouside the area and ghosted past Daniel Carvajal before tucking his shot past Keylor Navas inside the right post on 33 minutes.
Ter Stegen pulled off a super save to his left to deny Modric from 25 yards out three minutes later, while Alba scuffed a volley wide at the back post from Suarez's cross on the right on 43 minutes.
Barca should have taken the lead in stoppage time, but Messi somehow missed the target at the back post after Navas failed to deal with a corner from the left.
Ter Stegen was called into action just three minutes into the second half as he tipped Kroos' first-time shot from 19 yards out around the post, before being tested again on 53 minutes by Benzema's header from Marcelo's cross.
Carvajal went close moments later as he steered a cross narrowly wide of the right post from close range, while Alcacer drew a save out of Navas soon afterwards having latched onto a pass from Rakitic inside the box.
After Messi fired a free-kick over, Gerard Pique forced Navas into a save with a header from Rakitic's cross on 59 minutes, as the action swept from end to end.
Ronaldo then had two chances in the space of a minute as he connected with Marco Asensio's cross to fire wide of the left post, before the Portuguese forward blasted over from inside the box following a counter-attack.
Navas kept out Suarez and Messi in quick succession, while at the other end Marco Asensio almost beat Ter Stegen at his near post on 72 minutes, the keeper just getting down to block the strike from a tight angle on the right.
But there was nothing Navas could do to prevent the Catalans from going in front a minute later as Rakitic picked up a loose ball outside the area and cut onto his left foot before curling a shot into the left corner of the net.
Real looked to respond, with Ronaldo shooting straight at Ter Stegen after being teed up by Benzema just outside the box on 75 minutes, but Ramos' red card two minutes later - for a two-footed challenge on Messi - handed Barca an even bigger advantage.
Pique nearly made them pay on 80 minutes as he forced Navas into a smart save from close range after being picked out by Andre Gomes, while the Real keeper was at his best to parry Messi's low strike away from danger three minutes later.
A final roll of the dice from Zinedine Zidane saw him bring on Rodriquez, who quickly got into the action with a deflected shot that was held by Ter Stegen, and the move paid off within two minutes as the Colombian steered Marcello's cross home from six yards out at the near post.
Los Blancos pushed for a winner and Asensio forced Ter Stegen kept into another fine save at his near post in the 89th minute. It proved crucial as Barca finally took advantage of the extra man in stoppage time to score a dramatic winner.
Gomes played in Alba on the left and the defender cut the ball back for Messi to steer a shot into the bottom-left corner from 14 yards out.