Gavin Tomlin scored an injury-time winner as Gillingham beat Hereford in a remarkable game.
United led 2-0 with a Rob Purdie penalty and Tom Barkhuizen close-ranger but Chris Whelpdale's strike and Danny Kedwell's header levelled it.
Barkhuizen's shot and Will Evans's header again put the Bulls in control but Charlie Lee's brace in the last 10 minutes levelled the scores.
Purdie had an injury-time penalty saved before Tomlin volleyed home.
Relegation-battlers Hereford had looked good for a surprise win when they led by two within seven minutes.
Barkhuizen was fouled by Jack Evans for Purdie's penalty before the Blackpool loanee scored himself.
Whelpdale and Kedwell netted just either side of the break to level the scores for the promotion-chasing hosts.
But Hereford rallied and Barkhuizen bundled home before Evans's 80th-minute goal.
Substitute Lee scored within 60 seconds and again with two minutes remaining to send the teams level going into injury-time.
Hereford had a golden chance to win it when Paulo Gazzaniga fouled Barkhuizen but the goalkeeper made amends with a save from Purdie's penalty before Tomlin's volley sealed one of the most dramatic wins of the season.
VIEWS FROM THE DRESSING ROOM
Gillingham manager Andy Hessenthaler told BBC Radio Kent:
"I don't really want to go through that again. I'm thankful for the win but there's lots of lessons to be learned tonight.
"The boys kept believing and a lot of teams would have crumbled but in the end we got a win which was unbelievable.
"You're confident in Gavin Tomlin because he's a goalscorer and he's an intelligent player. My reaction? I've never jumped so high.
"It was entertainment but for a manager it was exhausting at times."
Hereford manager Jamie Pitman told BBC Hereford and Worcester:
"The lads worked hard but you can't give goals away like we have.
"It was a sending off on Sam Clukas, it's a disgraceful tackle, how the referee cannot warrant it was a goalscoring opportunity is beyond me.
"Things like that cost people their jobs, my boys are in there and they have families and kids to look after, so have I and that could cost me my job."