Bayern, who have lost two and drawn one of their last three league matches, found themselves down 3-0 after Bayer scored three goals in 16 second-half minutes and with just 20 minutes remaining.
Jürgen Klinsmann’s side fought back to make it 3-2 with two goals in the next four minutes but Bayer ensured they progressed with a goal in the second minute of time added on.
Bayer’s Swiss international Tranquillo Barnetta had opened the scoring in the 54th minute with a superb shot and they quickly doubled their advantage through a header by Arturo Vidal from a corner.
When Patrick Helmes added a third it looked like game over for Bayern, who had won four of the last six editions of the Cup.
But they showed some resilience in attack as Brazilian defender Lucio pounced to take advantage of a poor punch by German international goalkeeper Rene Adler to make it 3-1 and then Bayer’s nerves were set truly on edge as two minutes later Miroslav Klose scored following a terrific pass by Franck Ribery.
But it was still too little too late as Bayer – with three defeats, a draw and a win in their last five matches – added a deserved fourth goal.
Earlier on Wednesday both Hamburg and Werder Bremen had moved through to the semi-finals – Hamburg beating struggling Second Division Wiesbaden 2-1 while Werder Bremen were impressive 5-2 winners over VfL Wolfsburg.
Brazilian star Diego and Peruvian veteran Claudio Pizarro were the stars of the show for Werder as they grabbed a brace apiece.
Schalke 04’s disappointing season continued on Tuesday as they were knocked out of the German Cup 1-0 by Second Division Mainz in their quarter-final clash.
An 88th minute goal by Burkina Faso’s Aristide Bance was enough to end four-time Cup winners Schalke’s interest in the competition and send Mainz – who are second in their division – into the semi-finals for the first time in their history.
Schalke’s exit heaps the pressure on their Dutch coach Fred Rutten as they are eighth in the league, nine points adrift of leaders Hertha Berlin.