Andy Barcham bagged the only goal of the game as Gillingham dumped Bury out of the FA Cup.
The Tottenham Hotspur loan striker struck with just 19 minutes remaining to the delight of Gills boss Mark Stimson.
It was Barcham's second goal of the season and one which came against the run of play as the Shakers dominated proceedings.
The hosts started brightly and had a good chance in only the second minute.
Andy Morrell turned his marker and saw his powerful left-foot shot deflected wide for a corner.
At the other end, Gary Mulligan headed over at the near post from John Nutter's corner wide on the left.
Ryan Cresswell saw a glancing header deflected wide for a corner as the hosts pushed for the opener and, moments later, the Shakers thought they had the lead, only for Paul Scott's back-post header to be disallowed for a foul by referee Andy Haines.
Elliott Bennett should have given Bury the lead seven minutes before the break, but the midfielder fired high and wide with a left-foot shot from the edge of the area.
Efe Sodje headed a Brian Barry-Murphy corner into the path of Creswell in the 42nd minute, but the tall centre-back horribly miscued his shot and the chance went begging.
A minute later, match-winner Barcham forced a fine save from Wayne Brown from a powerful header as the visitors countered, before Andy Bishop twice went close for Bury.
The striker first forced Alan Julian to produce a good block from distance, before firing wide with a left-foot shot on the stroke of half-time.
Bishop had a penalty appeal turned down in the first minute of the second half after a clash with Gillingham skipper Barry Fuller, before the striker teed up Andy Morrell, but his strike partner saw his right-foot shot deflected wide for a corner.
Curtis Weston hit a curling left-foot free-kick straight at Brown, before Barry-Murphy's right-wing corner was headed from Creswell who, racing into the box, should have done better.
Then the hosts were hit on the counter-attack as Curtis Weston broke down the right and crossed for Barcham to head beyond Brown and into the far corner.
The goal was enough for a place in the next round.