Barcelona s Ivorian midfielder Franck Kessie celebrates after scoring his team s second goal during the Spanish league football match between FC Barcelona and Real Madrid CF at the Camp Nou stadium in Barcelona on March 19, 2023. Photo: AFP
The victory dents Madrid's ambitions of retaining the title and leaves the Catalans close to lifting the trophy for the first time since 2019.
Carlo Ancelotti's Real took the lead through a Ronald Araujo own goal but Barcelona pulled level when Sergi Roberto tucked home just before half-time.
Both sides had chances to win the game and Marco Asensio struck for Madrid, but the goal was ruled offside by VAR, before Alejandro Balde set up Kessie in the 92nd minute.
The build-up to the match was overshadowed by Barcelona's corruption charge in the "Caso Negreira" refereeing scandal.
Madrid joined Spanish prosecutors' complaint against Barcelona last week and their president Florentino Perez snubbed the game for the first time in over 20 years, amid rising tensions between the two Spanish giants.
That fed the match's intensity, both on and off the pitch, in Xavi's sixth Clasico as Barcelona coach but only his first at Camp Nou.
Xavi asked fans to make the stadium a pressure cooker and they responded, selling it out, whistling Madrid in possession and conjuring one of the best atmospheres of the season.
Madrid did not manage a single shot on target in their Copa del Rey semi-final first leg defeat by Barcelona earlier in March, but Karim Benzema stung Marc-Andre ter Stegen's palms inside the first minute.
Robert Lewandowski responded at the other end by testing Thibaut Courtois as the veteran marksmen exchanged shots.
When Barcelona conceded the opening goal of the game, it was friendly fire. Araujo deflected Vinicius' cross in at the near post, with Ter Stegen helpless.
Araujo, playing at right-back to counteract the Brazilian, has enjoyed his duels with Vinicius in the past but this time the winger triumphed, albeit fortuitously. Barcelona supporters became enraged by his emphatic celebration in front of them.
It was the first goal from open play Barcelona have conceded at Camp Nou in La Liga all season long, and only the second strike at home and ninth overall.
Courtois, who made an excellent save to keep out an early Raphinha header, could only watch as Andreas Christensen met another cross unmarked, but nodded off-target.
The Danish centre-back came close again with a flicked effort from a corner, before Courtois tipped over another stinging Raphinha drive, the Brazilian in overdrive on the right in Ousmane Dembele's absence.
Barcelona complained defender Nacho, already booked, should have been sent off for a hand-off on Raphinha.
The hosts did not get their wish, but were building up steam and found the breakthrough just before half-time, from an unlikely source.
Selected in place of injured midfielder Pedri, Sergi Roberto was in the right place at the right time to control a loose ball in the box and arc it beyond Courtois.
Barcelona had significantly more possession than in their cup win over Madrid and significantly more chances.
Lewandowski sent a deflected piledriver narrowly wide as Madrid wobbled, prompting Carlo Ancelotti to roll the dice and bring on Rodrygo and Ferland Mendy.
It almost paid instant dividends as Madrid robbed Sergio Busquets and Rodrygo lashed inches over the bar.
Madrid substitute Asensio fired home what might have been the winner but the goal was ruled out for offside after a nervy wait for VAR instruction.
Barcelona seemed content with the draw but Kessie's last-gasp strike sparked wild celebrations at Camp Nou.