The Bavarians, who face Liverpool in the Champions League last 16 next month, took the lead with their first shot on goal, Thiago Alcantara firing in from 12 metres.
But instead of a comfortable afternoon, the hosts were made to work hard.
"We started well, scored the goal but then were absent for 35 minutes," said Bayern coach Niko Kovac, who fielded the same starting lineup as in their win over Hoffenheim last week.
"We were too slow, too many players behind the ball and struggled to break the Stuttgart defence. We did it better after the break, we were faster, sharper. We can tick this game off, we stayed in touch (with Dortmund) and on we go."
Stuttgart snatched a 26th minute equaliser through Tasos Donis' superbly curled effort before Christian Gentner's 55th minute own goal, a deflection of Serge Gnabry's shot, gave Bayern a renewed lead.
The visitors refused to give up and hit the woodwork with Nicolas Gonzalez on the hour.
Bayern forward Robert Lewandowski sent his 65th minute penalty onto the post but Leon Goretzka settled the hosts' nerves, heading in with 20 minutes to go for his fifth league goal of the season.
Lewandowski then made amends for his earlier miss in the 84th as Bayern completed their win to move up to second on 42 points.
Dortmund are on 48 after crushing Hanover 96 5-1 on Saturday. Borussia Moenchengladbach are third on 39, having beaten Augsburg 2-0.
(Reporting by Karolos Grohmann; Editing by Christian Radnedge)
