Poland international Lewandowski struck in the 77th minute, flicking in a Kevin Grosskreutz shot with his heel for his 20th league goal of the season that lifted Dortmund to 69 points and left Bayern in second place on 63.
Bayern, whose nine-game winning streak was snapped, missed the chance to level five minutes from time when Arjen Robben had his penalty saved by Dortmund goalkeeper Roman Weidenfeller.
"The final minutes were unbelievable," Dortmund coach Jurgen Klopp told reporters.
"I think we deserved the win and we could not have played better. Now our friends at Schalke await us," he said of Saturday's Ruhr valley derby.
With Germany's football royalty in the stands and 200 countries tuned in for the big match, Dortmund, unbeaten now for a club record 24 straight games, were the better side throughout, even though playmaker Mario Gotze was ruled out after recovering from as groin injury only days ago.
It took the hosts only two minutes to slice open the Bayern defence with a superb through ball by Ilkay Guendogan for Jakub Blaszczykowski to slide the ball past keeper Manuel Neuer but narrowly wide.
Four minutes later Neuer twice denied the hosts, saving Grosskreutz's point-blank effort and then blocking Lewandowski's rebound shot as the atmosphere among the sold-out 80,720 crowd heated up.
The Pole, Dortmund's top scorer, then rattled the post with a powerful header in the 38th as Bayern struggled to keep up with the pace.
The visitors' biggest chance came on the hour but Franck Ribery's low drive from a tight angle sailed just wide of the post before Lewandowski struck 13 minutes from time.
Bayern poured forward for the equaliser and had a golden opportunity to level when Robben was brought down by Weidenfeller five minutes from time.
The Dutchman's weak spot-kick was saved by the keeper and minutes later Robben again missed from three metres out, somehow managing to fire over the bar.
Lewandowski again hit the woodwork when he chipped Neuer in stoppage time with the ball bouncing off the bar.
"This is extremely bitter and disappointing as I had scored the last 10 or 11 penalties," Robben told reporters. "Now they are almost champions."
Hannover 96 beat VfL Wolfsburg 2-0 to boost their chances of European football next season as they moved up to seventh place on 44 points leaving their opponents in 10th on 40.
Nuremberg took a big step towards staying up with a gutsy 4-1 win over third-placed Schalke 04 who have 57 points, four ahead of fourth-placed Borussia Monchengladbach, who drew 2-2 at Werder Bremen on Tuesday.
Hamburg dropped deeper into relegation trouble after being crushed 4-0 by Hoffenheim for their fifth defeat in seven games to remain only two points off the relegation play-of
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.