The away side took the lead after 11 minutes when Freddie Burns kicked over a penalty.
Nick Evans drew Quins level with a kick of his own before Matt Hooper touched down to extend their lead. Evans was successful with the conversion to make the score 10-3.
Another Burns penalty drew the visitors closer, while James Simpson-Daniel's try, converted by Burns, saw them take an 11-10 lead at the break.
A penalty from Nick Evans gave Harlequins a 13-11 lead early in the second half, but a kick from Burns saw Gloucester establish a narrow 14-13 advantage.
The decisive blow came in the final five minutes when Mike Brown capitalised to touch down before an impressive Evans conversion made the score 20-14.