Murray, who had been pushed to three sets by Mayer the only other time they had met, found himself facing an opponent playing to the best of his ability and needed all his mental reserve to overcome the German and rack up his 400th career win.
"I felt like I hit the ball very well today, very few miss hits, I just didn't return so well. That was the only thing I would have liked to have done better," he said.
"I was struggling - I think both of us were - at the end of the first set. There were so many long points."
Looking comfortable on serve during the first set, Murray enjoyed a couple of early opportunities to break the Mayer serve.
But the 29-year-old, happiest on clay of all the surfaces, proved strong enough to withstand the Murray onslaught and instead forced the epic first set tie-break.
Leading 4-2 at the change of ends in the breaker, Murray suddenly suffered a slight lapse in concentration as Mayer battled his way back into things only to squander five set points and his chance to really apply to pressure to the world number three.
Despite denying Murray on a first set point, the Scot was not about to waste his second chance and eventually wrapped up the first set (with an inside-out cross-court forehand winner), which at 71 minutes was just 10 minutes shy of Roger Federer’s entire match earlier in the day.
The second set was far from comfortable for Murray either as he quickly found himself a break down after failing to maintain the momentum gained from taking the first set.
Instead Mayer started to run away with proceedings, quickly racing out to a 4-1 lead before the Scot could haul himself back into the match; Mayer almost seemed lulled into a sense of lethargy as Murray also struggled under the Madrid sun to gather any kind of energy.
In the end though, the Scot has come through many a tight match, especially when not playing at his best, and it was this tenacious streak that saw Murray level up at 4-4 before holding to force the second tie-break four games later.
This time there were no mistakes from the world number three as he set up a third round clash with 19th seeded Gilles Simon at the first attempt with a backhand passing shot winner down the line.
"Every time you play against him there are a lot of long games and points, so you need to be prepared for that," Murray said. "I've played well against him in the past, so I hope I can do the same again on Thursday."
Second round results
1-Novak Djokovic (Serbia) 7-6(6) 6-7(8) 6-3
3-Andy Murray (Britain) beat Florian Mayer (Germany) 7-6(11) 7-6(3)
2-Roger Federer (Switzerland) beat Radek Stepanek (Czech Republic) 6-3 6-3
Fernando Verdasco (Spain) beat 12-Milos Raonic (Canada) 6-4 2-6 7-6(7)
16-Gilles Simon (France) beat Jeremy Chardy (France) 6-4 7-6(5)
Daniel Gimeno-Traver (Spain) beat 8-Richard Gasquet (France) 7-5 3-6 6-4
Pablo Andujar (Spain) beat John Isner (US) 6-4 6-4
First round results
Tommy Robredo (Spain) beat Marcos Baghdatis (Cyprus) 6-4 6-2
Viktor Troicki (Serbia) beat Marcel Granollers (Spain) 7-5 4-6 6-2
13-Tommy Haas (Germany) beat Andreas Seppi (Italy) 6-1 6-2
15-Stanislas Wawrinka (Switzerland) beat Marius Copil (Romania) 6-4 6-4
Mikhail Youzhny (Russia) beat Fabio Fognini (Italy) 7-6(4) 2-6 7-6(5)
Juan Monaco (Argentina) beat 9-Janko Tipsarevic (Serbia) 7-6(5) 6-3
Santiago Giraldo (Colombia) beat Martin Klizan (Slovakia) 6-2 6-4
Benoit Paire (France) beat Joao Souza (Brazil) 6-1 7-6(0)