James Walker, on loan from Charlton Athletic, snatched an 88th-minute winner to end a lean spell for Yeovil Town as they moved back into the top half of the League One table.
It was a heart-breaking defeat for new Gillingham boss Mark Stimson though, as his team had looked destined to pick up only their second point on the road this season when Chris Dickson scored a 76th-minute equaliser.
That effort had wiped out Lloyd Owusu's eighth goal of the season that had given Yeovil the lead ten minutes earlier.
With the rain pouring down, referee Andy Hall made three pitch inspections before giving the game the go-ahead.
It was Yeovil's 200th Football League match, but a combination of the rain and television cameras contributed towards their lowest ever attendance of 4,048.
The Gills had early shocks. On nine minutes skipper Ian Cox went off injured and his replacement, Efetobore Sodje, was only on the field for five minutes before he clashed heads with Lloyd Owusu.
The game was held up for ten minutes while Sodje was treated and in all the first half lasted 58 minutes.
There were no goals in the first half, although Yeovil hit the bar twice. The deadlock was broken on 66 minutes when, after a great run down the right, Walker produced a perfect cross for Owusu to tap the ball home.
Dickson had been Gillingham's most dangerous striker and it was he who bundled in the equaliser from close range after Yeovil had failed to clear a corner.
However, justice was done when Walker picked up a pass from loanee Darren Way and blasted home from the edge of the box.