Robert Lewandowski came off the bench to help 10-man Bayern Munich win 5-4 at home to Heidenheim in a remarkable DFB-Pokal quarter-final.
Leon Goretzka's early header had Bayern in front at the Allianz Arena on Wednesday but Niklas Sule was sent off and Robert Glatzel and Marc Schnatterer scored to give the second-tier side a shock half-time lead.
Lewandowski was benched due to illness but within 11 minutes of his introduction as a half-time substitute he had set up Thomas Muller and turned in from close range to put Bayern ahead.
Serge Gnabry seemingly ended Heidenheim's hopes by converting a corner but Franck Schmidt's men improbably hit back with Glatzel scoring twice to draw the scores level at 4-4 and complete his hat-trick.
There was more drama to come, though, as referee Guido Winkmann harshly pointed to the spot when Marnon Busch handballed in the box, Lewandowski staying calm to settle matters with six minutes to play.
Bayern join RB Leipzig and Hamburg in the draw for the semi-finals, but this was more exertion than Niko Kovac would have wanted ahead of Saturday's crunch clash with Bundesliga title rivals Borussia Dortmund.
Bayern took the lead in the 12th minute when Goretzka rose unmarked to head home Joshua Kimmich's corner but moments later they were reduced to 10 men.
Sule brought down Robert Andrich as he threatened to burst into the box and, following a VAR check, referee Winkmann changed his initial yellow card decision to a red.
Heidenheim captain Schnatterer smashed the resulting free-kick against the crossbar with Sven Ulreich, again deputising for injured captain Manuel Neuer, beaten by the powerful strike.
Kovac sent on Jerome Boateng to replace Franck Ribery but Heidenheim levelled in the 25th minute, Glatzel climbing at the back post to head a deep Schnatterer cross home.
And the second-tier side took the lead before half-time, Sebastian Griesbeck evading Rafinha to set up Schnatterer for a smart finish past Ulreich.
Lewandowski and Kingsley Coman were introduced at the break, with Muller volleying in the equaliser from the former's clever header in the 53rd minute.
An amazing game in Munich sees Bayern progress to the semis— The DFB-Pokal (@DFBPokal_EN) April 3, 2019
Heidenheim will no doubt be disappointed but can certainly feel proud of the valiant effort they put in #FCBFCH #DFBPokal, you've done it again pic.twitter.com/UbghVvoTz9
Bayern then retook the lead, Gnabry leading a break to free Muller, whose cross-shot was turned home from close range by Lewandowski.
Gnabry seemingly guaranteed Bayern's place in the last four by turning in from Mats Hummels' flick-on, but Heidenheim hit back again.
Glatzel rifled home his second to halve the deficit, then when Hummels grabbed the arm of Maurice Multhaup as he entered the Bayern box, the striker coolly converted a penalty.
Gnabry crashed against the bar before Ulreich denied Denis Thomalla in a one-on-one in a frantic finish that eventually saw Heidenheim heats broken, Busch unfortunate to be punished for handball and Lewandowski stroking home his second of the game to send Kovac's men through.
What does it mean? Lucky escape for Bayern
Heidenheim deserved their half-time lead and Bayern were staring down the barrel of a humiliating DFB-Pokal exit ahead of the crucial visit of Dortmund at the weekend before Lewandowski's impact.
Kovac's future has been the subject of much debate for most of his first season in charge and a defeat here would have piled pressure on the former Eintracht Frankfurt coach, who lifted this trophy last season before joining beaten finalists Bayern.
Lewandowski makes the difference
Muller struggled as a solo striker in the first half and Lewandowski's arrival immediately sparked life into the Bundesliga giants. His impact after the restart was swift and, after Heidenheim battled back, he stayed cool to book a place in the semi-finals.
Bayern have now reached the last four of the DFB-Pokal for 10 successive years but they likely would not have done so had Lewandowski not shaken off his illness.
Sule dismissal leaves Bayern in trouble
Centre-back Sule had never been shown a red card in his career but a rash tackle on Andrich, when Bayern seemed to have the game under control, put Kovac's men in a tight spot.
It is arguable whether it was the correct use of VAR for a decision that was subjective and Sule will be grateful his colleagues ensured his dismissal was not as costly as it could have been.
Dortmund are the visitors to the Allianz Arena on Saturday for a game that could decide the Bundesliga title. Heidenheim's promotion push continues at home to Cologne a day later.