Goals from Claudio Pizarro and Thiago Alcantara secured a 16th win of the season for the league leaders but they only clicked into gear in the final 15 minutes of the game on Wednesday.
Pizarro headed Bayern's equaliser in the 76th minute and a sensational acrobatic effort from Thiago sealed all three points in the last minute of stoppage time.
Stuttgart took the lead just in the 29th minute through Vedad Ibisevic, the striker reacting quickest to fire past Manuel Neuer.
The hosts should have doubled their lead on a number of occasions, and they were made to rue those chances as Bayern snatched victory late on.
Bayern's win extends their lead at the top of the table, and ensures their 43-game unbeaten run in the league continues.
Bayern named an unchanged side for the first time in Pep Guardiola's reign, with rumoured Juventus target Mario Mandzukic named as a substitute.
Stuttgart brought Rani Khedira and Timo Werner into their starting line-up and the latter should have opened the scoring in the eighth minute.
After Mohammed Abdellaoue lost possession inside the penalty area, the loose ball fell for the 17-year-old but he blazed over the crossbar without testing Neuer.
Bayern survived a penalty shout against Rafinha four minutes later, Werner's header struck the right-back's arm inside the area, however referee Manuel Grafe was not convinced and waved home appeals away.
Guardiola's side were far from their normal fluent self and were punished in the 29th minute as Ibisevic netted his 10th league goal of the season.
Werner's deflected shot from the edge of the area dropped to the Bosnian striker via the boot of Abdellaoue, who looked offside, and he calmly fired past Neuer from 10 yards to give the hosts a deserved lead.
Thiago and Xherdan Shaqiri both went close to levelling for Bayern in the closing minutes of the first half but were unable to beat Ulreich.
Ibisevic had a chance to double his tally, and Stuttgart's lead, early in the second half but he fired wide of goal after a mistake from Rafinha.
With 54 minutes played, the hosts had another chance to score, and they would have made it 2-0 had it not been for a goalline clearance from Jerome Boateng.
Former Barcelona man Thiago and Thomas Muller were denied by Ulreich but substitute Pizarro had no problems beating the goalkeeper with a well-placed header in the 76th minute.
Fellow substitute Mandzukic had a chance to win the game in the closing minutes but Thiago spared Bayern's blushes with an acrobatic volley in the last seconds of the match.
Get the best features, fun and footballing frolics straight to your inbox every week.
Thank you for signing up to Four Four Two. You will receive a verification email shortly.
There was a problem. Please refresh the page and try again.