The on-loan Arsenal striker fired home from close range to put the visitors ahead after five minutes following good play by Uche Ikpeazu.
Congolese midfielder Amine Linganzi scored his first Gillingham goal on the half-hour mark to bring the sides level, heading home from Elliott Hewitt's looping cross.
However, Aneke latched on to Matt Tootle's pinpoint delivery eight minutes after the break to head home his 16th goal of the season, before Matthias Pogba sealed the win with a brilliant back-flick from six yards.
The Railwaymen ended the game with 10 men as Anthony Grant picked up two bookings for dissent in two minutes late on.
Gillingham had won five out of their previous six games at home but were outplayed by their opponents who claimed victory for the fourth time in as many games at Priestfield.