Brentford lost top spot in League One after a mix-up involving goalkeeper Stuart Nelson and Sam Sodje.
Nelson hit a clearance against Sodje on the stroke of half-time, leaving Neil Harris with a simple chance.
Martin Allen's side hit back after 74 minutes when 18-year-old debutant
Ryan Peters headed home Jay Tabb's pinpoint cross at the far post.
The visitors were cursing at being denied a penalty when Tabb appeared to handle Darren Byfield's effort.
Brentford: Nelson, O'Connor, Sodje, Turner, Frampton, Peters, Lewis (Dobson 62), Hutchinson, Tabb, Owusu, Rankin (Campbell 63). Subs Not Used: Fitzgerald, Gayle, Bankole.
Goals: Peters 75.
Gillingham: Brown, Rose, Cox, Hope, Sancho (Flynn 86), Jackman, Jarvis, Pouton, Crofts, Byfield (Shields 89), Harris. Subs Not Used: Hessenthaler, Knowles, Hislop.
Booked: Pouton, Rose, Byfield.
Goals: Harris 45.
Ref: P Melin (Surrey).