The Catalan giants were able to hang on to a slender aggregate lead to reach the final four of the cup, knocking out Jose Mourinho's brave men in what was an end-to-end encounter
Despite Madrid being the better side for the majority of the first half, the home side grabbed the lead in the 43rd minute when Pedro coolly slotted home after an excellent Lionel Messi run. The Catalans were in wonderland just before the half time whistle when Dani Alves unleashed a stunning drive into the top corner of the net.
Cristiano Ronaldo pulled a goal back in the 68th minute and four minutes later the visitors were level when Karim Benzema slammed home. Yet it was not to be for the away side, who had Sergio Ramos sent off late on for a second yellow card as Barca held on for a place in the last four, where they will face either Valencia or Levante.
Madrid nearly made a dream start after only 12 seconds, with the Barcelona defence clearly not at the races. Dani Alves attempted to play the ball back to Gerard Pique, but the stopper did not have his eyes on the play and allowed it to bypass him into the path of Gonzalo Higuain, who curled his hurried effort wide of the post.
Jose Mourinho's men continued their electric start and squandered another glorious opportunity in the third minute. Xabi Alonso whipped an inviting free kick into the danger area which bypassed the home side's defence, and only a smart reaction save by Jose Pinto could deny Higuain's header from point-blank range.
The visitors were impressive in the first quarter of the game and were within inches away of opening the scoring in the 25th minute. Mesut Ozil picked the ball up 30 yards from goal and unleashed a venomous drive which had the beating of Pinto before rebounding off a combination of crossbar and post to safety.
However, for all of their endeavours, los Blancos found themselves a goal down in the 43th minute after some Lionel Messi magic. The diminutive Argentine weaved his way past several Madrid players before playing a clever pass to the unmarked Pedro, an early substitute for the injured Andres Iniesta, who swept the ball home past a helpless Iker Casillas from 12 yards out.
Despite coming under pressure for the majority of the half, Barcelona grabbed a stunning second three minutes into injury time. Xavi's free kick deflected off the chest of Lassana Diarra and into the path of Dani Alves on the edge of the box. The Brazilian smashed an unstoppable shot into the top corner of the net to give his team a 4-1 aggregate lead at the break.
Mourinho's side, and their attacking approach, remained undeterred into the second half. Sergio Ramos thought he had pulled a goal back in the 53rd minute, slamming a header past Pinto from a Xabi Alonso set-piece, yet the goal was disallowed by the referee as the former Sevilla man was adjudged to have tugged at Alves' shirt in the build-up.
There was to be a ray of light for the visitors as they scored in the 68th minute. Ozil's precise through ball fell to the feet of Ronaldo who rounded Pinto before calmly sliding into the empty net.
Four minutes later Madrid were level on the night when Gerard Pique's clearance was headed into the path of Benzema by Xabi Alonso. The French striker, who came on as a substitute, flicked the ball past Carles Puyol before slamming it home to set up a thrilling end to a superb encounter.
Both sides refused to sit back in the final stages as Madrid looked for the one goal they needed while, predictably, Barcelona used attack as the best form of defence.
However, the scoreline stayed the same and Madrid's frustration led to Ramos being sent off in the 88th minute for a second yellow card after raising an arm to Sergio Busquets, as the Blaugrana squeezed through to the final four of the Copa del Rey. Madrid, however, can take pride in what was a hugely impressive performance.
Both sides return to Liga action at the weekend with Real Madrid entertaining Real Zaragoza on Saturday while Guardiola's side travel to Villarreal.