Leon Goretzka put the visitors ahead in the 39th minute after an earlier effort by Thiago Alcantara was ruled out following the use of the video assistant referee.
Mainz did not have to wait long for an equaliser, with Jean-Paul Boetius striding into the box in the 48th to drill in a cross after being left completely unmarked.
Spaniard Thiago got his goal in the 62nd minute to put Bayern back in front and Javi Martinez hit the woodwork a little later as Bayern hung on to their slim lead and the three points.
"It was a hard-earned win for us and it was certainly not as sparkling as we had hoped it would be," Bayern coach Niko Kovac told reporters.
Thiago jubelt (1. FSV Mainz 05 vs. FC Bayern München)
Image credit: Getty Images
"But in the current situation we are happy with the three wins in a row. It was still a deserved win and we hope it continues like that."
Bayern, who have been under pressure after a dip in form early in the season, lacked a clear game plan, especially in attack, and never really hit top speed against lowly Mainz.
"I was not nervous after they equalised," said Kovac. "But I was angry because we did not do what we set out to do after the break. We are caught napping during a cross and then bring ourselves into difficulty."
The champions are now on 19 points in second place, two behind Dortmund, who drew 2-2 against Hertha Berlin.
The Bavarians have now won all three games in all competitions since club bosses launched a verbal attack on the media last week, blaming them for what they said was unfair reporting during their four-game winless run.