Gavin Tomlin scored in the dying seconds against Hereford to seal all three points in an incredible 5-4 victory for Gillingham at the MEMS Priestfield Stadium.
Paulo Gazzaniga had already saved a penalty in the 90th minute to keep the scores at 4-4 - Charlie Lee having netted twice in nine minutes after coming on as a substitute at half-time.
Hereford scored twice in the opening seven minutes to take an early lead, but Chris Whelpdale scored on the stroke of half time to give Andy Hessenthaler's side some hope.
Danny Kedwell levelled things up just one minute in to the second half, but the Gills found themselves 4-2 down with a little over 10 minutes to go.
Will Evans and Tom Barkhuizen had both scored before the referee awarded a penalty in the last minute to the visitors with the scores level.
Gazzaniga received a booking for bringing down Hereford's top scorer, but he saved brilliantly to clinch what seemed to be a solitary point.
However Tomlin had other thoughts and with seconds left he finished on the turn with his right foot to beat Dave Cornell for the fifth time to seal an unbelievable victory considering the Gills were not in front until the last moments.
Hessenthaler was given the luxury of being able to name the same team for a fourth successive game.
Simon King kept his place in the centre of defence, but fellow defenders Joe Martin and Garry Richards were unable to take their places on the bench.
Hereford came into the game without a victory in their last six outings.
They made two changes to their starting line-up - Danny Williams and Michael Townsend replacing Ben Purkiss (suspended) and Nicky Featherstone.
In as early as the third minute the Gills fell behind when Jack Evans was guilty of a mistimed tackle on Barkuizen just inside the penalty area, and the referee didn't hesitate in pointing to the spot.
Rob Purdie, who missed a spot-kick when the two sides met in September, converted the spot-kick to give the visitors an early advantage.
A couple of minutes later the player was receiving treatment on the side of the pitch after receiving a blow to the face, but he was able to continue.
Five minutes later and the Gills were given a mountain to climb when top scorer Barkhuizen got a second for Hereford.
He skipped past Evans with ease and slotted to the left of Gazzaniga to put the visitors two up inside the first 10 minutes.
Gillingham seemed stunned by the early proceedings and struggled to get a foothold on the game.
The visitors looked the more threatening and were winning the majority of 50/50 balls.
On 19 minutes the hosts had their first opportunity. Whelpdale received the ball 25 yards from goal and attempted an audacious lob to catch out Cornell.
However the Hereford stopper wouldn't be called upon as the ball went over the crossbar.
Six minutes later Tomlin had a great chance to reduce the deficit.
The ball was played to him well inside the penalty area on the left-hand side - he managed to get away a powerful effort but Cornell made himself big and the ball rebounded off him and eventually away to safety.
By now the Gills were starting to boss the game and began creating numerous chances.
On 33 minutes Tomlin thought he had given the home side a real lifeline. Evans burst through on the right hand side and waited patiently before delivering a ball across the box for the striker.
He put the ball beyond the keeper and was about to celebrate before noticing the linesman had raised his flag for offside.
Lyle Taylor could have made it 3-0 in the 38th minute following a good cross from former Gills striker Delroy Facey, but the ball was slightly behind him and all he could do was direct it goalwards and see it caught by Gazzaniga.
Two minutes before the break Whelpdale came close once more. A Danny Jackman corner was headed out by one of the Hereford defenders, and the midfielder swung his left foot at it and caught the ball well.
Cornell didn't move and was relieved to see the ball go inches wide of his left hand post.
However Whelpdale did reduce the arrears moments later. Jackman again with the cross from the left and he found the midfielder in space having out run his marker.
He then had the simple task of powering home from close range. The half-time whistle followed soon after.
Gillingham made a change at half time, Evans, who recovered well after a poor start, was replaced by Charlie Lee.
Within 60 seconds of the restart he had an immediate effect. His long throw caught the Hereford defence napping and Kedwell was on hand to hook the ball beyond Cornell and level up the scores from six yards out.
It was the striker's 13th goal of the season and couldn't have come at a better time.
Kedwell nearly had his second on 58 minutes. His strike on the edge of the box was a clean one, but sadly for him it lacked accuracy and Cornell wasn't called upon.
Curtis Weston was next to have a go a couple of minutes later. In amongst a host of players he was able to get a powerful right foot effort away.
Cornell though refused to be beaten for a third time and got down very quickly to beat it away.
On 63 minutes Tomlin came centimetres away from putting the Gills ahead. After managing to keep the ball in at the left-hand corner flag he delivered a cross right across the face of the goal, and with no one on hand to convert it whistled past the post as another opportunity went begging.
Hereford did create chances - one of which fell to Will Evans in the 68th minute. Facey held the ball up on the edge of the area and playing a delicate ball into his path. He struck well but went a yard or so over the bar with Gazzaniga at full stretch.
On 70 minutes the visitors regained the lead. A Will Evans shot from 25 yards was only parried out by Gazzaniga, and Barkhuizen tapped in his second from just less than six yards to silence the home supporters.
The goalkeeper's movement suggesting the ball had moved in the air, but the defence were unable to get back quick enough to prevent the goal.
The visitors got their fourth in the 80th minute. A Delroy Facey cross from the byline was hooked towards the far post, and Will Evans did well to get his head on it and divert beyond Gazzaniga who did his best to save to his right hand side but couldn't keep it out.
At 4-2 down it looked like the game was done, but within one minute of the kick-off Charlie Lee brought one back.
He went on a fantastic solo run, beat a number of players on the way to the penalty area and struck early with his left foot which went in off the post.
Evans could have got his second with four minutes left to play - he waited for the ball to cut across him and then let fly with a 25-yard effort which Gazzaniga did well to tip over.
That miss would prove costly as Lee got his second in the 88th minute. He skipped past the Hereford full back, got into the box and smashed his right foot shot past Cornell who conceded for the fourth time.
There was still time for more drama though as Hereford won a penalty in the last minute. Barkhuzien was brought down by Gazzaniga and the Argentine was booked for his trouble.
He would make amends though as he saved the resulting spot kick magnificently to keep the hosts in the game.
The save would prove to be crucial as Tomlin got the winner deep into stoppage time.
Jo Kuffour had broken through and was looking for support - the ball worked its way in front of Tomlin, and on the turn he finished spectacularly to claim the winner in an enthralling encounter.
Gills: Gazzaniga, Evans (C Lee 45), King, Essam, Jackman, Tomlin, Weston (Montrose 77), Payne, Whelpdale (O Lee 77), Kuffour, Kedwell.
Unused subs: Hawkes, Davies.
Hereford: Cornell, Anthony, Williams (Clucas 74), Townsend, Purdie, Facey, Lunt, Clist, Evans, Barkhuizen, Taylor (Colbeck 82)
Unusued subs: Bartlett, Elder, McQuilken.
Referee: Mr D Phillips
Attendance: 3,784 (45 Hereford)
Bookings: Cornell (45) C Lee (78), Gazzaniga (90), Montrose (90) Tomlin (90)
Goals: Purdie 4 (pen), Barkhuizen (7, 70),W Evans (80) Whelpdale (45), Kedwell (46) C Lee (81, 88), Tomlin (90)