Leon Goretzka's volley put the visitors ahead after a sustained period of attacking pressure but Bayern's defensive frailties were exposed by a fast Mainz break and a Jean-Paul Boetius goal just after half-time.
A draw would have been a disappointing result for Kovac's men, who went into the game trailing league leaders Borussia Dortmund by four points, but Thiago's first league goal of the season helped them to a third successive win in all competitions.
The hosts set their stall out to defend and packed their penalty area but Joshua Kimmich shook the frame of the goal after 26 minutes when he sent a swerving drive onto the crossbar.
After VAR was used to rule out a Thiago close-range volley that found the net through a crowded box, Bayern opened the scoring on 39 minutes when Kimmich's neat cross from the right was volleyed home smartly by Goretzka from 10 yards out.
Within three minutes of the restart Mainz were level, Daniel Brosinski's cross from the right reaching the far post where Boetius shrugged off Kimmich before beating Manuel Neuer low to the goalkeeper's left with a side-footed finish.
But just after the hour mark, Bayern netted again.
Robert Lewandowski scampered down the right side of the penalty area and whipped in a low cross that found Thiago, who slid in and guided the ball into the left corner of the net to allow the visitors, their coach and their supporters to breathe a sigh of relief.
Muller back to his best
Thomas Muller's return to the side was a welcome one, the experienced midfielder stroking the ball around confidently and providing Bayern with a near-constant out-ball.
Kimmich caught napping
Despite his valuable contributions to Bayern's attack, Kimmich is a defender and he was slow to react to Mainz's fast break early in the second half, allowing Boetius to get ahead of him and score the equaliser.