Hampton entered the game with a less-than-spectacular 6-8 record and 5.36 ERA compared with Cain's 12-2 record and 2.12 ERA.
"So many times I've been one pitch away from a quality start," said Hampton, who returned to the Astros this season after pitching for them from 1994-99. "I made that pitch tonight and all that emotion came out, to see all that hard work pay off."
Hampton, characteristically, did give up a run in the first inning on three singles.
"That's been my whole career," he said of his traditional problems in the first. He gave up another in the third on two more singles, an error by himself and a sacrifice fly to make it 2-0.
Geoff Blum gave Houston some hope of keeping Cain from winning his league-leading 13th game when he homered to lead off the bottom of the fifth.
"You've got to hope he makes a mistake," Astros manager Cecil Cooper said of facing Cain. "He got Blum to chase some pitches down in the zone. He came back to it and Blum went down and got it."
The key for Hampton was surviving the sixth inning.
He hit Ryan Garko to start the inning and, after striking out Aaron Rowand, gave up a double to Edgar Renteria. The Astros walked Randy Winn intentionally to bring up Cain.
Hampton struck out Cain and Eugenio Velez to escape the inning and keep the Astros within a run.
"Mike was bending, but not breaking," Cooper said. "The last three or four innings he really battled. He did a good job for us."
"He threw a lot of strikes, a lot of fastballs, a lot of fastballs in," Rodriguez said of Hampton. "The big thing was his control."
Hampton (7-8) left after six innings, giving up eight hits but only one earned run.
"It's a big win because [Cain's] been phenomenal this year," said Hampton, who benefited from some good defense, including right fielder Hunter Pence throwing out Renteria at home in the second.
"That's what it takes," Hampton said. "They're going to put a lot of balls in play when I pitch."
Michael Bourn opened the last of the sixth with a triple to right-center off Cain. Matsui followed with his fifth homer of the season, hitting a 3-2 pitch off the foul pole in right.
"I was looking for fastball," Matsui said through an interpreter. "It was inside. I was hoping it wouldn't go foul. I was relaxed at the plate because Michael Bourn [has] speed at third. Even if I hit a ground ball, Michael Bourn could score with the infield back."
Cain didn't think either home run was a bad pitch.
"I thought the one to Blum was a good pitch; he just got the barrel to it. And then the one to Matsui, I haven't taken a look at, but I assume he just got the head [of the bat] on it," Cain said.
Of the eight hits Cain allowed over eight innings, five were for extra bases.
"He keeps you off-balance," said Bourn, who hit a single as well as a triple in four at-bats against Cain. "His fastball is sneaky. Everything else is off-speed. He brings it every night. We battled against him. In the eighth inning, he's still throwing 95, 96 [mph]."
The Astros stretched their lead to 4-2 in the eighth on a two-out triple by Pence and a single by Carlos Lee to score a run the Astros needed when closer Jose Valverde (save No. 13) gave up a run in the top of the ninth.
Lee went 3-for-4 off Cain with a double and two singles.
"In that situation, I wasn't thinking he was going to give me anything to hit," Lee said of his hit in the eighth that drove in Pence from third. "I got lucky. His fastball is so explosive."
"That run obviously went on to win the game," Cain said. "That is a big run to keep it 3-2, and I left a slider up a little bit and he was able to bloop it over the infield."
Cooper was happy to see his offense produce four runs off a pitcher like Cain, especially after the Astros had scored three or fewer runs in eight of the last nine games, seven of them losses.
"We've been scuffling to score runs," Cooper said. "Carlos put a real good swing [on a couple of pitches] and Michael got a couple of good swings on him."