Vinicius Junior put them in front in the 25th minute before Luuk de Jong equalized a while later for Blaugrana.
Karim Benzema then restored Madrid's lead midway through the second period but Ansu Fati brought Xavi's side level again with a header.
Extra-time beckoned after the game finished 2-2 and Federico Valverde made it 3-2 for Los Blancos in the 98th minute, from which there was no coming back for the Catalans.
Real Madrid have won their fifth Clasico in a row and now play either Atletico Madrid or Athletic Bilbao in the Supercup final on Sunday.
Here are the player ratings for Barcelona and Real Madrid:
Barcelona Player Ratings
Marc-Andre ter Stegen - 6/10
Another indifferent night in goal for the German, whose positioning for the opening goal in particular was poor.
Dani Alves - 5/10
Barcelona fans were excited to see him play in the Clasico again but Alves was well off the pace. Vinicius had no issues drifting past him all night.
Ronald Araujo - 5.5/10
The Uruguayan's positioning was suspect whenever Los Blancos came forward in attack but still managed to make one clearance and four tackles.
Gerard Pique - 5/10
Another Clasico veteran, Pique, also had a poor game. Real Madrid's forwards twisted him inside out with their movement and deadly link-ups.
Jordi Alba - 6/10
The Spaniard struggled to make any notable impact either side of the field but assisted Fati for a late equalizer.
Gavi - 6/10
The youngster was a non-factor attacking-wise but must be credited for dealing excellently with Toni Kroos.
Sergio Busquets - 5/10
He gave away possession for Real Madrid's opening goal and was then hopelessly off the pace in the recovery.
Frenkie de Jong - 4.5/10
The Dutchman was totally anonymous in the opening stanza and was hence taken off at the break.
Ferran Torres - 5/10
His Barcelona debut couldn't have been any worse as Torres never troubled Real Madrid and he too went off at half-time.
Luuk de Jong - 7/10
The Dutchman had a bright game and the striker constantly tested Real Madrid with his shots. Three games in a row now that he's scored a goal in!
Ousmane Dembele - 7.5/10
The Frenchman looked revitalized on the night, causing the Whites all sorts of problems down the left wing despite never scoring or assisting.
Pedri - 7.5/10
A fireball of energy and full of spirit, Pedri made an instant impact. He created a chance for Dembele right after coming on and then saw a shot fly narrowly wide.
Barcelona would have been pleased with his impact.
Abde Ezzalzouli - 6/10
The youngster was unfazed by the occasion but appeared to overexert himself a few times.
Ansu Fati - 7/10
He scored a crucial headed goal which sent the game into extra time. Barcelona will be pleased to see Fati among the goals once again.
Memphis Depay - 6/10
The Dutch striker couldn't get as involved as Barcelona would've liked.
Nico Gonzalez - 6/10
He didn't do much wrong but didn't particularly stand out either.
Ferran Jutgla - N/A
He came on for the final 10 minutes of the match but was barely seen.
Real Madrid Player Ratings
Thibaut Courtois - 6.5/10
Teh Real Madrid goalkeeper was beaten twice on the night but nearly got his hands to Barcelona's first goal and made some key saves in the match otherwise.
Dani Carvajal - 5/10
Sloppy in possession and no match for Dembele - Carvajal had a torrid game.
Eder Militao - 4.5/10
The Brazilian was beaten for both of Barcelona's goals in a rare poor outing.
Nacho - 5/10
He didn't cover himself in glory either, looking shaky in defense and lacking the solidity that Real Madrid boasted this season.
Ferland Mendy - 5.5/10
The Frenchman couldn't bring anything meaningful to Real Madrid's attack and was caught out of position a few times too.
Luka Modric - 7.5/10
Graceful and effortless as ever, with the Croatian keeping it all ticking for Real Madrid.
Casemiro - 6.5/10
The Brazilian was a good protective cover for the back-four but he made a few rash tackels and even got a yellow card.
Toni Kroos - 6.5/10
As mentioned before, Gavi negated his influence, so Kroos couldn't have a big impact despite laying off so many accurate passes.
However, the German's passing still gave Real Madrid some much needed control.
Marco Asensio - 6.5/10
Movement - excellent. Awareness - good. End product - lacking.
Karim Benzema - 8/10
Benzema saw a shot come off the post and then another one parried away by Ter Stegen but was on hand to fire home the rebound through a cross from Carvajal. He knows no stopping - 23 goals for the season now!
Vinicius Junior - 8/10
He dismantled Alves clinically and also fired the Whites in front with a composed finish beyond Ter Stegen for the opener.
Rodrygo - 6.5/10
He assisted Valverde for the winner with a simple square pass but missed an easy chance late on.
Federico Valverde - 7.5/10
The Uruguayan was an unlikely hero for Los Blancos with a winning goal in extra-time.
Lucas Vazquez - 6/10
Besides a few ordinary passes and forward runs, there was nothing much to see from him.
Eduardo Camavinga - N/A
Not enough time to make an impact.