A Thomas Müller penalty and Mario Gómez brace put Bayern 3-0 ahead with half-an-hour to play but Stuttgart made it a nervy finish following a Martin Harnik double.
Bayern had the first chance, with Arjen Robben exchanging passes with Franck Ribery and bearing down on Sven Ulreich, who was able to palm the Dutchman's cross-shot around the post.
Gradually, though, Stuttgart were able to get a foothold in the game and, were it not for some wasteful finishing, they could well have gone ahead.
Bayern were left furious on 31 minutes when Robben was scythed down by Serdar Tasci, only for the referee to give a corner rather than a penalty, but their protests were soon forgotten.
Soon after, Philipp Lahm charged into the box and went down under pressure from Traore. The official duly awarded a spot kick and Muller stepped up to send Ulreich the wrong way.
Bayern could have been home and hosed in stoppage time when the ball fell kindly to Gomez in the box but Antonio Rudiger got across to make a vital block and keep the underdogs in it.
Any hopes they had of Pokal glory were extinguished minutes into the second half, though. Robben slipped the ball into the right-hand channel to Lahm, who squared the ball for Gomez and the former Stuttgart striker was left with the simple task of bundling into an empty net.
Bruno Labbadia's men had the wind knocked out of their sails and, soon enough, it was three. Muller got down the right once more and squared it to Gomez, who grabbed his second.
Alaba had a shot charged down second later and Heynckes' side were threatening to run away with it, but Stuttgart saved some face with 19 minutes remaining, as Gotoku Sakai found Harnik with his cross, who headed home to muted celebration.
The Austrian soon raced down the right and squared it to Shinji Okazaki and claims for a penalty against Jerome Boateng were waved away before Harnik set up a grandstand finish, lashing home the rebound off his own shot, which Neuer had parried.
Bayern were made to endure a nervous ending to the game but held on to make history.