Ritchie capped an eye-catching individual display by scoring the Robins’ winner just before the hour, drilling in a fine shot from the edge of the area that flew in at the far post.
Ashley Vincent blasted over from close range early on for Colchester following Kayode Odejayi’s run from inside his own half.
Swindon were a whisker away from taking the lead after 17 minutes when Aden Flint’s header crashed against the underside of the crossbar and Matt Heath headed Oliver Risser’s follow-up off the line.
A minute later, Ritchie looked set to score when he pounced on Risser’s pass and rounded goalkeeper Ben Williams, only to fire into the side-netting.
Robins substitute Ronan Murray fired straight at Williams early in the second half following Lukas Magera’s defence-splitting pass. But Ritchie gave Swindon the lead soon afterwards and the League Two side were worth of their place in the third round.