A victory would have confirmed Bayern Munich a record-extending ninth-consecutive Bundesliga title. In a bright start by the visitors, Benjamin Pavard flashed a shot narrowly wide.
However, Bayern Munich went behind in only the third minute when Jonathan Burkardt's strike from the edge of the area went in off a weak attempted punch by Manuel Neuer.
The Bayern Munich custodian made some fine saves after that, while Robert Lewandowski blazed over from a promising position.
Mainz were undeniably the better side in the game's opening exchanges and got a deserved second when Robin Quaison rose highest to thunder home a header from Philipp Mwene's free-kick.
That gave Mainz a two-goal lead heading into half-time. Interestingly, in their first meeting of the season, Bayern Munich also trailed by two goals at the break only to emerge victorious 5-2.
Meanwhile, Hans-Dieter Flick responded to the lethargic performance of his team in the first half by making three changes at the break. Leroy Sane, Kingsley Coman and Leon Goretska went off for Eric Maxim Choupo-Moting, Tanguy Nianzou and Jamal Musiala, respectively.
The second half was played at a slower tempo, with Mainz content to sit back and protect their two-goal lead, while Bayern Munich struggled to generate any rhythm.
Frustration slowly crept into the Bavarians' game as several players were booked for cynical fouls. They were, however, gifted a goal when Alexander Hack's weak clearance allowed Lewandowski to steal in behind the defence and half Bayern Munich's deficit.
However, that was almost the last kick of the game, and Mainz hung on for an invaluable three points in their quest to avoid the drop.
On that note, let's have a look at the Bayern Munich player ratings n the game.
Manuel Neuer - 6/10
The Bayern Munich goalkeeper made a horrendous error to gift Mainz an early lead. But Manuel Neuer made amends by making a string of fine saves.
Alphonso Davies - 6.5/10
Alphonso Davies utilised his pace to mop up when Bayern Munich were caught out while pushing forward in the second half. Other than that, it was an underwhelming outing from the young Canadian.
David Alaba - 5.5/10
David Alaba failed to judge the flight of the ball in the lead-up to Mainz's second goal and also got booked in the first half.
Jerome Boateng - 6/10
Jerome Boateng made three interceptions and two clearances on the night. He was also the first player to go into the referee's books, though.
Benjamin Pavard - 6.5/10
Benjamin Pavard offered more of a threat in attack for Bayern Munich than Davies. The France international also performed admirably in his defensive duties.
Leon Goretzka - 5.5/10
Leon Goretzka struggled to impose himself in the opening 45 minutes and was booked for a mistimed tackle. It did not come as a surprise that he did not take the field for the second half.
Joshua Kimmich - 6.5/10
Joshua Kimmich was overrun in midfield in the first half but helped Bayern Munich regain control in the second half. He ended the game with four chances created for his teammates.
Kingsley Coman - 5.5/10
Kingsley Coman was ineffectual in the 45 minutes he spent on the field and struggled to capitalise on the opportunities when he was played through.
Thomas Muller - 6/10
Thomas Muller was one of Bayern Munich's brightest sparks in attack, but his finishing left a lot to be desired.
Leroy Sane - 5.5/10
Leroy Sane was bright and quick down the flanks, but his decision-making and finishing in the final third were way off. He was one of three Bayern Munich players substituted at half-time.
Robert Lewandowski - 7/10
The Poland international was on the periphery for most of the game and missed a golden opportunity in the first half. His killer instincts were on full display, though, as he pounced on a defensive error by Mainz to score his 36th league goal of the campaign.
Ratings of Bayern Munich Substitutes
Eric Maxim Choupo-Moting - 6/10
Eric Maxim Choupo-Moting was unfortunate to drop to the bench following Lewandowski's return to the XI. In his 45 minutes on the field, Choupo-Moting registered two of Bayern Munich's four shots on target.
Tanguy Nzianzou - 7/10
The 18-year-old Tanguy Nzianzou came on at half-time and proved his defensive mettle with two interceptions and a tackle.
Jamal Musiala - 6/10
Jamal Musiala did not get many chances on the night to showcase his prowess in front of goal for Bayern Munich.
Serge Gnabry - 6/10
The Germany international came on with 20 minutes to go. Serge Gnabry had a pass accuracy of 91.7%.