Andre Schuerrle and Florian Heller scored the late goals that secured a come-from-behind victory for Mainz, who are chasing a top three finish for a Champions League place.
Hamburg were extremely lucky to take the lead through Marcell Jansen's shot from 12 metres came down off the bar but clearly did not cross the line as TV replays showed.
The home side's luck ran out in the second half though when Schuerrle and Gojko Kacar's own goal either side of Mladen Petric's 59th minute strike saw Mainz level.
The visitors went ahead for the first time when Schuerrle picked up the ball near the halfway line, sprinted past the hapless Kacar and chipped goalkeeper Frank Rost in the 82nd.
The 20-year-old Germany international then threaded a precise pass between Rost and two defenders for Heller to tap in Mainz's fourth goal two minutes from time.
The victory saw Mainz move to 43 points, four behind third-placed Hanover 96 who beat Bayern 3-1 on Saturday leaving the Munich side on 42 points.
Borussia Dortmund are cruising towards their first title since 2002 with a 12-point lead after beating Cologne 1-0 on Friday while Bayer Leverkusen, with 49 points, tightened their grip on second with a 3-0 win over VfL Wolfsburg on Saturday.
In Sunday's early game, late goals from Peruvian Claudio Pizarro and Marko Marin gave Werder Bremen a 3-1 victory at mid-table Freiburg to move out of the relegation zone.
Bremen, who grabbed their first win in seven league games and are battling to avoid the drop after playing Champions League football earlier this season, are now on 28 points in 14th place, three points above the relegation playoff spot.
"We finally managed to get three points. You need recognition for your work and we have not had that recently," said Bremen coach Thomas Schaaf.
Sandro Wagner gave the visitors the lead after 16 minutes when Freiburg cleared a Pizarro header off the line but the 23-year-old striker was on hand to score from close range.
Freiburg levelled just after the break with a penalty from Papiss Demba Cisse, who now has 17 league goals this season, one less than Bundesliga top scorer Mario Gomez at Bayern.
But with eighth-placed Freiburg increasing the pressure late in the game, Marin broke free, fed Pizarro with a well-timed pass and the Peruvian, who is the Bundesliga's all-time top foreign scorer, fired in a low drive after 76 minutes.
Germany international Marin turned scorer deep in stoppage time to seal Bremen's first win since mid-January.
Get the best features, fun and footballing frolics straight to your inbox every week.
Thank you for signing up to Four Four Two. You will receive a verification email shortly.
There was a problem. Please refresh the page and try again.