Federer, who is using a new, larger racquet, struggled for much of the match and his opponent had more breaks – five to the Swiss’ four – and won more points.
The four-time Hamburg winner won the first set on a tie-break but home hope Mayer raced into a 5-1 advantage in the second set before the Swiss won two games on the spin to cut the arrears to 5-3.
However, a double fault and two forehand errors from the former world number one saw Mayer take the second.
In the third, Federer broke service twice only to see Mayer break straight back but when he took a third break to lead 6-5 in the decider he served to love to secure a spot in the semi-finals.
Federer will face qualifier Federico Delbonis, who beat 14th-seeded Fernando Verdasco 6-7 (5-7) 7-6 (10-8) in a three hour and 14 minute epic.
Second-seeded Tommy Haas lost to Fabio Fognini 6-2 6-4 – the 12th-seeded player is looking for his second straight title having won in Stuttgart.
The first set was an even affair with both players having four breaks points but Fognini prevailed having converted two of his to take the first set.
Fognini then closed out the second set 6-4 to take the match in one hour and 21 minutes.
ATP Hamburg quarter-final results
1-Roger Federer (Switzerland) beat Florian Mayer (Germany) 7-6(4) 3-6 7-5
12-Fabio Fognini (Italy) beat 2-Tommy Haas (Germany) 6-2 6-4
Federico Delbonis (Argentina) beat 14-Fernando Verdasco (Spain) 6-7(5) 7-6(8) 6-4
3-Nicolas Almagro (Spain) beat 5-Juan Monaco (Argentina) 4-6 6-0 6-3
ATP Bogota quarter-final results
2-Kevin Anderson (South Africa) beat 6-Santiago Giraldo (Colombia) 6-3 6-3
Ivo Karlovic (Croatia) beat 5-Adrian Mannarino (France) 7-6(5) 7-6(3)
- Sports & Recreation
- Fabio Fognini
- Tommy Haas
- Florian Mayer