Substitute Deon Burton's penalty helped Gillingham overcome Dagenham and reclaim top spot in League Two.
Leon Legge scored his first Gills goal from close range after Myles Weston's free-kick deflected his way, despite appeals for offside.
Femi Ilesanmi levelled for the Daggers when he pounced on Legge's poor clearance to fire home after the break.
However, Burton slotted home from the spot after keeper Chris Lewington had fouled Anton Robinson to seal the win.
Defeat for their closest rivals Port Vale meant that the Gills moved to the top of the League Two table for the first time since January.
VIEW FROM THE DRESSING ROOM
Gillingham manager Martin Allen told BBC Radio Kent:
"It was defending at its best. Everyone to a man stood firm, stood their ground and reduced them to very few chances.
"Our players responded very well (to the equaliser), stepped up another gear, got the goal, and we did what you could call park the bus. We shut up shop and the players kept going till the end.
"To hear the noise of the stadium, the atmosphere, and to send the supporters home happy is magic."
Sorry, we were unable to load match stats, please try again.