Two goals from Freddy Eastwood helped secure Grays’ place in the next round of the FA Cup.
But Blues made it hard for themselves, allowing their rivals to equalise twice before the eventually stepped up a gear at Clarence Park.
Grays went ahead after just seven minutes when Martin Carthy fired past Barry White in the home side’s goal.
But City came back in the 20th with a Graeme Tomlinson penalty.
Grays retook the lead in the 37th minute when Eastwood’s near-post shot scudded in, and the tit-for-tat goal scoring continued as City’s Scott Oakes scored just on the break with an inch perfect chip.
Sloppy defending in the second half gave Freddy Eastwood a chance to score his second and the visitors made sure of the game five minutes from time.
Stuart Thurgood lobbed the keeper and left a hapless City defender unable to clear.
Thanks to the Thurrock Gazette for the match report