The Robins manager saw the best and the worst of his team during a tense 90 minutes, but emerged smiling after Simon Gillett's 25-yard rocket after 83 minutes had settled the outcome.
Cotterill had blasted his players after a 3-0 defeat at Sheffield United in their previous game and said: "There will probably be a lot more ups and downs between now and the end of the season.
"We have started the game really well and some of the football we played in the first half would have been the envy of a top-six team.
"Then we made what seems to be our mandatory mistake defending a corner and allowed Gillingham back into the game.
"I'm delighted for Simon Gillett because I thought he was having an excellent game even before his goal. It was a fantastic strike.
"Our other goal was also out of the top drawer and it's a much happier dressing room than it was a week ago.".
With the visitors looking the more likely winners, Gillett, on loan from Nottingham Forest, unleashed a spectacular shot with his right foot that ripped into the top corner of the net.
Sam Baldock had broken the deadlock for the hosts after 27 minutes, chesting down a precise chip forward from Wade Elliott and hammering a sweetly-struck right-footed volley past Stuart Nelson from 15 yards for his 18th goal of the season.
Gillingham improved after the break and deservedly equalised in the 65th minute when Ade Akinfenwa headed back across goal from a right-wing corner and substitute Charlie Lee blasted home from close range.
City made a bright start, which saw Bobby Reid just fail to connect with a Baldock cut-back and Jay Emmanuel-Thomas fail by inches to slide in and convert Reid's cross.
Gillingham found it hard to create first-half chances. Their best effort was a fierce 51st-minute shot by Cody McDonald, which was well smothered by Frank Fielding.