"Guys like that have to make mistakes in order for you to beat them," Houston manager Cecil Cooper said. "You don't get a whole lot off guys like that. We made it all count."
Blum led off the fifth with a homer to right, the first earned run allowed by Cain in 14 innings. That cut the Giants' lead to 2-1 and Matsui put Houston on top in the sixth with a two-run shot off the right-field foul pole.
Cain (12-3) also gave up Carlos Lee's RBI single in the eighth and the Giants lost for the first time in the last six games he started.
"I just got lucky there," Lee said. "I guess it was our night. He was pitching really good. Even the balls we hit were good pitches."
Cain went 3-0 with an 0.94 ERA in six July starts. He allowed only four earned runs the whole month.
"He still pitched a good ballgame," Giants manager Bruce Bochy said. "He gave up a couple of long balls, but pitched well enough that he should've gotten the win."
Mike Hampton (7-8) gave up eight hits, only one after the Giants took a 2-0 lead in the third. He struck out six and won for just the third time in eight starts.
Hampton plunked Ryan Garko with a pitch in the sixth and one out later, Edgar Renteria doubled over Lee's head in left. Hampton intentionally walked Randy Winn to load the bases, then struck out Cain and Eugenio Velez.
"It was a fun win," Hampton said. "It was one of those games you really want to be involved in."
The Giants led 1-0 in the first after consecutive singles by Sandoval, Molina and Garko.
Hampton allowed runs in the first inning for the 12th time in 19 starts. He came in with an 11.50 ERA in the first inning (23 earned runs), the highest in the majors among pitchers who started at least 15 games.
Renteria singled in the second, advanced on Cain's bunt and tried to score on Velez's single to right. Hunter Pence's throw was off line, but catcher Ivan Rodriguez caught it and tagged out Renteria near the plate.
The play inspired Hampton.
"They're going to put a lot of balls in play when I pitch," Hampton said. "Great throw by Hunter, as usual. He doesn't miss by much when he does and tonight he threw one right there."
Freddy Sanchez singled in the third and went to third when Sandoval's hard grounder ricocheted off Hampton to the side of the mound. Molina's sacrifice fly made it 2-0.
Blum's sixth homer of the season was only the second allowed by Cain in seven starts.
"I thought it was a good pitch," Cain said, "one of those pitches that you get away with."
Michael Bourn led off the Astros' sixth with a triple to right center, his NL-leading ninth of the season. Matsui homered off the right-field foul pole, his fifth of the season and second in five games.
Cain had not allowed two homers in a game since June 9.
"He's got great velocity, a good slider and curve," Matsui said through an interpreter. "His control is good, too. I have to be patient."
Pence tripled to center in the eighth and Lee drove him in with a single to right. Houston's eight hits were the most allowed by Cain since Seattle had 10 in the Giants' 5-1 win on May 23.
- Houston RHP Roy Oswalt felt pain in his lower back after a bullpen session and will skip his scheduled start on Tuesday. Forced to leave his previous outing because of an irritated disk, Oswalt said he would meet with doctors on Tuesday and throw another bullpen session on Wednesday.
- Cain failed in his bid to become the fourth pitcher in Giants history to win 13 of his first 15 decisions in a season.