Preston led for most of the first half thanks to captain John Welsh’s early strike but Cody McDonald equalised just before the break.
However, North End struck twice in six second-half minutes through Gallagher and substitute Neil Kilkenny to climb up to third in the table.
Preston took the lead in the eighth minute through Welsh who shot past goalkeeper Stuart Nelson from 12 yards after a Stuart Beavon lay-off.
Gillingham were struggling to compete and team-mates Chris Whelpdale and Joe Martin were spoken to by referee Charles Breakspear after a heated disagreement.
However, the visitors were level on the stroke of half-time when Jack King miscued a headed clearance and Leon Legge fed McDonald who lashed home a shot from eight yards out.
Preston regained the lead after 57 minutes as Beavon found Gallagher on the edge of the area and he curled in an effort from 18 yards.
And it was 3-1 to six minutes later when sub Kilkenny scored with his first touch, bundling home from three yards out after Joe Garner’s effort had been saved by Nelson.