Gillingham extended their unbeaten run to eight league and cup games as big striker Mamady Sidibe grabbed an 86th minute winner to dent Leicester's chances of closing the gap on leaders Portsmouth.
Leicester suffered an early blow, losing ten-goal Brian Deane after only five minutes with a fractured cheekbone, but battled back well with goals by Frank Sinclair and young substitute Tommy Wright.
Both were set up by Alan Rogers and Gillingham keeper Jason Brown kept his side level with a superb save to deny Rogers and Wright.
Gillingham, without in-form striker Marlon King, sidelined for up to eight weeks with a knee operation, recalled Mali international Sidibe and he played alongside Guy Ipoua, reuniting the partnership that served Gillingham so well at the start of the season when they won their opening three games.
Deane was hurt following a clash of heads with Gillingham defender Chris Hope and looked dazed as he made his way off the pitch and back to the dressing room accompanied by the Gills club doctor.
Wright came on and got a 25th minute booking for a late challenge on Nicky Southall and Leicester were second best throughout the first half.
Hope headed an early free-kick at Ian Walker, Andy Hessenthaler had a shot blocked and then Walker saved a deflected Paul Smith shot at the second attempt.
Gillingham won their first corner just after the half hour and after Leicester failed to clear it, Hessenthaler crossed from the right, and the ball fell to Paul Shaw who drove a rising 20-yard drive past the Leicester keeper.
Gillingham then added a second when Sidibe raced for a long ball with Walker inside the box and the Gills man won possession and hit a low drive goalwards which Leicester defender Matt Elliott attempted to clear but only managed to hit it into the roof of the net.
Leicester came out desperate to get themselves back into the contest. They won a free-kick which Rogers took and Sinclair reacted first nodding the ball past Brown.
Hessenthaler then had a seemingly goalbound shot bounce off Sinclair before Leicester equalised with Wright's first senior goal, tucked in from almost ground level as Gillingham failed to defend a low cross by Rogers.
Five minutes later Brown came to Gillingham's rescue with a double save to deny first Rogers and then Wright.
Leicester looked favourites to go on and claim the points but the arrival of ex-Leeds and Bolton man Rodney Wallace as an 81st minute sub for the tiring Ipoua proved to be the turning point.
Hopes's long ball forward caught Walker in two minds and Wallace, fit again after hamstring trouble, rounded the former England man and crossed for Sidibe to score into an empty net.