This was Leverkusen's their first victory in Munich since October 1989.
Stefan Kiessling gave Leverkusen the lead in the 42nd minute before Mario Mandzukic equalised after 77 minutes. But the game was decided when Jerome Boateng was unlucky to see Sidney Sam's header rebound off him at close quarters and fly in.
Bayern dropped their first points of the season after eight straight victories, but still lead the league with a four-point advantage over closest challengers Schalke.
Leverkusen ended their 22-match winless streak against Bayern in Munich to move up to fifth.
Bayern coach Jupp Heynckes made two changes to his line-up due to injuries with David Alaba coming in for Franck Ribery and Luiz Gustavo replacing Javi Martinez.
The hosts started the better team and Bastian Schweinsteiger tried this luck with a back-heel after two minutes but his attempt went just wide left.
Bayern had another chance in the 14th minute but Leverkusen keeper Bernd Leno made a fine save against Mandzukic.
Leno again displayed his fine form five minutes from the break, fending away Schweinsteiger's dangerous attempt.
Leverkusen then grabbed the lead in the 42nd minute as Bayern captain Philipp Lahm tried to clear Andre Schurrle's cross from the right but Kiessling was there to deflect the ball in at the left post.
Bayern came out of the break and pushed forward one wave after another.
Leno kept out Boateng's long-distance blast in the 54th minute and cleared Xherdan Shaqiri's drilled effort on the hour.
Heynckes brought on Claudio Pizarro in the 74th minute for Schweinsteiger and the move paid off right away as the Peruvian set up the equaliser with a pass for Mandzukic to head in.
Pizarro nearly gave Bayern the lead in the 79th minute but his shot deflected off Mandzukic before striking the right post.
Leverkusen got the second goal with Sam heading Gonzalo Castro's cross from the right side off the face of Boateng and past a wrong-footed Manuel Neuer.
There was enough time for Bayern to almost steal a point back but Pizarro found the crossbar with a header in the third minute of injury-time.