Thiago Alcantara’s winner, which also marked his first goal for Bayern, was a superb volley in the third minute of time added on to give the champions their 16th win in 18 matches.
“We tried everything right up until the end, so the victory is deserved,” said Spain star Alcantara.
“Yes, it was a beautiful goal from me, but the success of the team is far more important.”
Bayern were 14 minutes from seeing their record run — dating back to October 2012 — come to an end before Claudio Pizarro’s headed equaliser cancelled out Vedad Ibisevic’s first-half goal.
“When it’s 1-1 right up to the end, then a win is definitely welcome,” said Bayern captain Philipp Lahm.
“We had a few good chances before that, but Stuttgart made it hard for us and played really well.”
This was a re-arranged fixture from December when Bayern were in Morocco winning the Club World Cup, but the Bavarians did not have things their own way for a change.
Bayern are on a host of league records — this was their 28th away match without defeat.
And with 50 points from 18 games, coach Pep Guardiola has made the best start of any coach in Bundesliga history.
“Stuttgart played very well for 30 minutes, they were more aggressive and we had trouble finding our lines,” said Guardiola.
“We created more chances in the second half, but the order in our game wasn’t so good.
“At the end of the day, we are happy. That was a fantastic goal and an unbelievable win.”
Bayern fell behind when 17-year-old left winger Timo Werner drove in a shot, which fell to the feet of Stuttgart’s Mohammed Abdellaoue.
The Norway international, looked to have been offside and replays showed he was, but the flag stayed down and Ibisevic slotted home his pass on 29 minutes.
In response to falling behind, Bayern raised the tempo.
Only a fine save from Stuttgart goalkeeper Sven Ulreich five minutes before the break denied Alcantara, who unleashed a ferocious strike from well outside the area.
Guardiola trudged off for the half-time break with a face like thunder.
The game turned bad tempered after the break with referee Manuel Graefe dishing out three yellow cards in quick succession.
Both sides had clear chances with Bayern centre-back Jerome Boateng clearing off the line from Rani Khedira, brother of Real Madrid midfielder Sami, on 54 minutes.
At the other end, Thiago got in behind the defence, but Ulreich blocked the shot on 71 minutes for a corner.
Bayern poured on the pressure and Stuttgart’s resolve began to show signs of cracking.
Veteran Pizarro came off the bench and Guardiola’s choice paid dividends as he headed past Ulreich.
Werner squandered a great chance for the winner on 81 minutes when he fired straight at Manuel Neuer.
Tempers frayed as the final whistle loomed with Boateng and Stuttgart’s Martin Harnik clashing after the latter clattered into Dante.
Both Boateng and Harnik picked up bookings, as the referee ended up showing a total of eight cards — five for Stuttgart, three for Bayern.
Alcantara capped a fine display when he volleyed home Rafinha’s superb floated cross for a stunning goal to deny valiant Stuttgart even a point.