With the match deadlocked at 0-0 at the end of extra time, Thiago Alcantara converted the decisive penalty in the shootout after Bayern goalkeeper Manuel Neuer had saved Josip Drmic's effort.
"Penalties are always a matter of luck," said Bayern forward Thomas Mueller, who converted from the spot. "This match cost us a lot of nerves."
Earlier, Borussia Moenchengladbach crashed out also penalties against third-division Arminia Bielefeld.
The victory kept Pep Guardiola's team on course for a repeat of the 2013 treble. His team is the far away leader in the Bundesliga and is also in the quarterfinals of the Champions League. Bayern is seeking its 18th German Cup title.
In the semifinal, Bayern will host Borussia Dortmund, the team it beat in last year's final, while Bielefeld will be at home to Wolfsburg.
Guardiola watched the shootout calmly from a chair on the sidelines.
Both teams had chances to settle the issue earlier, with Mueller coming close late in the first half and Neuer foiling Karim Bellarabi after the break. At the other end, Bernd Leno parried Robert Lewandowski's effort, before Neuer denied Bellarabi once again in a pulsating match.
Bayern's Rafinha made a vital lunging clearance to prevent Julian Brandt from getting off a clean shot as the 90 minutes came to an end.
Marioe Goetze wasted Bayern's best chance in the 98th, missing the target from close range while unchallenged at the far post.
Bayern played without the injured Arjen Robben, Franck Ribery and Bastian Schweinsteiger.
Bielefeld won its shootout 5-4 after holding the third-place Bundesliga team to a 1-1 draw over 90 minutes and then extra time. The winner reached the semifinals for the third time after 2005 and 2006, when it was still in the top tier.
Moenchengladbach's Raffael sent the opening penalty wide and Ibrahim Traora had the decisive shot saved as the third-division leader advanced.
Manuel Junglas put Bielefeld ahead in the 26th and Max Kruse equalized from the penalty spot in the 32nd.
Bielefeld had already eliminated Bundesliga sides Werder Bremen and Hertha Berlin and is undefeated in 17 home matches in the third division.
All content copyright ©2015 Daily Journal, a division of Home News Enterprises unless otherwise noted.