Cain (11-5) surrendered seven hits and a walk while striking out six in the outing a day after being honored in an ATT&T Park ceremony for his June 13 perfect game against the Houston Astros.
The right-hander responded by breaking a personal four-start winless streak during which he had allowed 15 runs over 25 2/3 innings.
Posey's shot in the third inning doubled the Giants' lead to 4-0 while Angel Pagan added a two-run triple for San Francisco, which came into the contest tied with the Los Angeles Dodgers for the National League West lead.
The victory broke a two-game Giants skid and evened their three-game set with the Rockies.
Colorado's Drew Pomeranz (1-7) was touched for four runs on six hits over four full frames.
DJ LeMahieu socked his first major-league homer in the loss.