The Sweden Under-21 international had failed to score in his previous 11 games for Gladbach, but ended the drought with a penalty just before half-time to cancel out Shawn Parker's opener.
Hrgota put the visitors in front in the 59th minute and, after Mike Hanke had added a third, completed his treble 10 minutes from time.
Andreas Ivanschitz got Mainz's second with an injury-time penalty, but it could not stop Gladbach keeping their, admittedly very slim, European hopes just about alive.
The game started at a frantic pace with Tony Jantschke shooting wide from close range for Gladbach and Julian Baumgartlinger bringing a fine save out of Marc-Andre ter Stegen at the other end.
Parker put Mainz ahead in the 12th minute.
Zdenek Pospech's long ball from the right split the Gladbach centre-backs and Parker took it down well before shooting home from a central position.
Juan Arango crashed a free-kick from the edge of the area off the bar as the visitors looked for a leveller and it arrived in the 39th minute.
Bo Svensson pulled back Hanke as he looked to get on to a pass from Hrgota and conceded a penalty.
Hrgota stepped up and nervelessly chipped his spot-kick down the middle.
There was a brief delay at the start of the second half because of flares in the Gladbach end, but their team were providing plenty of fireworks on the pitch.
Hrgota got his second in the 59th minute, pouncing to score after Hanke's header had come off the underside of the bar.
The young striker should have had a third a minute later, but he chose to try and go round goalkeeper Heinz Muller and the stopper got a finger to the ball and pushed it clear.
But Hanke did give Gladbach some breathing space in the 63rd minute, this time following up to head the ball over the line after Patrick Herrmann's effort had hit the underside of the bar.
And Hrgota completed his hat-trick in the 80th minute, Hanke's excellent through-ball playing in his strike partner, who, one-on-one with Muller, scored with a fantastic lob.
Ivanschitz got a second for Mainz from the spot after Alvaro Dominguez was penalised for handball, but it could not take the gloss off a hugely impressive afternoon for Gladbach.