Jonathan Burkardt stunned the visitors after three minutes, turning at the edge of the box and releasing only for Bayern keeper Manuel Neuer to turn the ball into his own goal.
The hosts enjoyed a powerful start and twice hit the woodwork in the opening 18 minutes with the Bavarian defence in disarray though Neuer made amends with a string of fine saves to keep the scoreline respectable.
The in-form hosts, unbeaten now in their last seven league games and having won three in a row, got a deserved second goal in the 37th when Robin Quaison drilled in a header from a free kick.
The Pole is chasing Gerd Mueller's all-time Bundesliga record of 40 goals in a season set in 1971/72.
"We had wanted a lot more from this game," Bayern captain Neuer said. "Especially in the first half it was far too little from us.
"It was clear that Mainz were in good form. We created far too few chances and were then punished at the back. I would have liked to have settled everything today. This is a heavy defeat for us after the past few weeks."
Bayern, who have three games left, are on 71 points, 10 clear of Leipzig, who play VfB Stuttgart on Sunday. The Bavarians will be champions if Leipzig lose.
Mainz, having played an outstanding second half of the season after collecting only seven points in their first 17 games, are now on 34 in 12th place and almost certain to stay up.