Danny Kedwell opened the scoring from the penalty spot in first-half stoppage time after City defender Liam Fontaine was booked for deliberate handball and debutant Ben Strevens gave the visitors breathing space.
City substitute Marvin Elliot headed home Brett Pitman's free-kick deep into injury time to set up a grandstand finish, but Allen's opposite number Derek McInnes was left with plenty to ponder.
Chances were initially at a premium, but Robins midfielder Martyn Woolford enjoyed his side's best two first-half moments.
The former Scunthorpe man fired just wide from John Stead's 22nd-minute knockdown and narrowly failed to make contact with Pitman's teasing cross moments later.
Dean Gerken was pleased to see Kedwell's header from Chris Whelpdale's cross fall wide after the half hour, but the City goalkeeper was culpable when League Two Gillingham went in front.
He spilt a Kedwell free-kick and, in the ensuing scramble, the prone Fontaine handled and the Gills striker made no mistake from 12 yards.
Allen's men doubled their advantage in the 54th minute. Charlie Allen's corner was glanced on by Andy Frampton and Strevens was on hand to provide the finishing touch with a deft flick.
Whelpdale might have compounded City's misery when he planted a near-post header into Gerken's arms and it was too little, too late by the time Elliot successfully marked his return from injury in the fifth minute of time added on.