The reigning champions, who wore 'Black Lives Matter' t-shirts ahead of the match, felt behind when Lucas Alario fired in after 10 minutes as Leverkusen, without the injured Kai Havertz, scored with the first chance of the game.
But Bayern recovered from the early shock and Kingsley Coman curled in the equaliser in the 28th minute before the visitors struck twice in three minutes to turn the game around.
"We needed a bit of time to get into the game and the goal was a wake-up call for us," Bayern coach Hansi Flick, whose team has won 23 of the 26 matches since he took over earlier this season. "The way we came back from a goal down shows the attitude and mentality of the team."
Bundesliga top scorer Robert Lewandowski headed in Thomas Muller's 20th assist of the season -- a Bundesliga record -- to seal Bayern's ninth straight win before a superb 89th-minute goal from 17-year-old Florian Wirtz cut the deficit for the hosts.
At 17 years and 34 days, the youngster became the Bundesliga's youngest ever scorer. Wirtz is 48 days younger than previous record-holder Nuri Sahin, who scored for Borussia Dortmund against Nuremberg on Nov. 26, 2005.