Mainz out of German Cup
Frankfurt took the lead on 13 minutes when Caio, who had come on for injured Alexander Meier only three minutes earlier, thundered in an unstoppable shot from 20 metres.
In-form Gekas doubled the lead with a well-taken header seven minutes later but Hamburg, missing injured Ruud van Nistelrooy, responded when Mladen Petric tapped in from close range.
The Croat should have levelled but hit the post with keeper Oka Nikolov beaten and Gekas punished the visitors with his second goal on the stroke of half-time before Petric headed in an own goal to make it 4-1.
The striker made amends with a fine header to cut the deficit but Halil Altintop converted an 87th-minute penalty, awarded for a foul on Gekas, to seal the hosts' place in the third round.
Bundesliga leaders Mainz 05 have won eight out of nine league matches this season but they were outclassed by Alemannia Aachen.
Hoffenheim, who played more than half the game with 10 men after striker Vedad Ibisevic was sent off, struggled against FC Ingolstadt but escaped with a 1-0 win thanks to Demba Ba's 63rd- minute winner from close range.
Holders Bayern Munich edged past last season's finalists Werder Bremen 2-1 on Tuesday and Schalke 04 struggled to beat FSV Frankfurt 1-0.