The tie had been evenly-poised after four goals were shared in the first leg of the semi-final in Germany last week.
Madrid had an impressive 13-month unbeaten home record and a wealth of European history behind them, but they were on the verge of a second consecutive last-four exit in this competition when Bayern substitute Alphonso Davies produced a 68th-minute rocket.
It stunned a previously buoyant home crowd, but Madrid boss Carlo Ancelotti worked his magic and turned to his bench to save the day.
Joselu answered the SOS as he made the most of a Manuel Neuer mistake to tap in the leveller after 88 minutes.
Three minutes later he steered in a second, which was originally disallowed before it was given after VAR intervened.
Ancelotti’s team withstood an extended period of stoppage time and Matthijs de Lift had a late effort ruled out for the visitors to ensure Madrid booked an 18th appearance in a European Cup final days after they clinched the LaLiga title.
It also meant England captain Harry Kane, who was subbed off with his team in the lead, will be consigned to a trophyless first season with the German giants.
The home fans were almost on their feet in the 13th minute, only for Neuer to show his class with a supreme double save.
Dani Carvajal picked out Vinicius Junior with an excellent pass through the legs of Noussair Mazraoui, but Neuer got down to push his low strike on to the post and reacted quickly to block Rodrygo’s follow-up effort.
Bayern’s task was dealt a blow with 26 minutes played when Serge Gnabry was forced off with yet another injury and Thomas Tuchel sent on reported Madrid target Davies.
Kane had been an isolated figure up to this point but in a flash demonstrated his threat when a sweet volley on the turn was tipped wide by Andriy Lunin two minutes later.
Neuer again came to the rescue of the German outfit five minutes before half-time when he tipped a dangerous cross by Vinicius wide, which ensured it was goalless at the break.
The second half started at a frantic pace and after Davies had a shot deflected over by Carvajal, Vinicius took it upon himself to force the issue and upped the ante against right-back Joshua Kimmich.
Bayern did weather the storm and spaces started to open up as a Jamal Musiala snapshot was tipped over Lunin.
A lack of composure in attack had hurt the visitors, but that changed in the 68th minute when they broke the deadlock through an unlikely candidate.
Kane was involved after he chipped out to Davies, who cut inside Antonio Rudiger and arrowed an effort into the top corner.
Madrid thought they levelled three minutes later, but Federico Valverde’s deflected strike from a corner was ruled out after Nacho pushed Kimmich to the floor in the build-up.
A rare sight of goal for Kane ended up with an effort off target before Tuchel made the surprise decision to withdraw the former Tottenham forward with five minutes left.
It backfired terribly when Los Blancos forced an equaliser with 88 minutes on the clock.
A 22-yard curler from Vinicius appeared routine for Neuer, but he spilled the shot and Joselu beat Dier to the loose ball to level.
The Bernabeu was a cauldron of noise now with the roof closed and Joselu had the ball in the net again in the first minute of stoppage time.
Madrid kept the attack alive and Rudiger crossed in for Joselu to steer home, but the joy was shortlived by the offside flag.
VAR told Szymon Marciniak to review the incident and replays showed both Rudiger and Joselu were onside to spark scenes of delirium as Madrid players raced around in celebration.
More drama was to follow when De Ligt fired into the bottom corner, but the offside flag had already been raised due to Mazraoui and, despite plenty of remonstrations by Bayern, it was disallowed to consign the German giants to a first season without silverware since 2012.