SAN FRANCISCO -- Matt Cain provided necessary competence as the game began. Brian Wilson added a dynamic conclusion.

In between was a mess. But the Giants didn't care. They outlasted the Arizona Diamondbacks on Sunday, 9-7, to end a three-game losing streak and avoid being swept.

San Francisco squandered a 5-1 lead and trailed, 6-5, before Jose Guillen's two-run single fueled a three-run seventh-inning rally. The resurgent Freddy Sanchez, who went 3-for-5, lined an eighth-inning RBI double to pad the Giants' lead and help fend off the D-backs, who belied their status as the National League West's cellar-dwellers by playing spirited baseball all weekend.

The Giants trimmed San Diego's division lead to five games and remained 1 1/2 games behind Wild Card leader Philadelphia. Nevertheless, Wilson said, "I personally don't really care what Philadelphia's doing and San Diego's doing. I just know that we play seven games against San Diego and we need to step it up and that's the only time we can worry about whether San Diego's going to be losing."

Though the Giants sustained a lively offense after entering the game with three runs scored in their previous 22 innings, Cain and Wilson quite literally bookended this much-needed triumph.

One day after manager Bruce Bochy lectured members of the Giants' starting rotation about their consistently poor performances, Cain halted the slide with a performance that outshone his pitching line -- three runs and six hits allowed in 6 1/3 innings. Wilson worked 1 2/3 shutout innings for his eighth save this year of at least four outs and 36th overall.

Initially, Cain appeared bound to continue the trend set by Giants starters, who allowed 13 first-inning runs in the previous three games. The D-backs loaded the bases with one out on Stephen Drew's single, Justin Upton's double and Chris Young's walk. Cain responded by striking out Adam LaRoche, who homered in each of the two previous games, and Mark Reynolds.

"What he did there probably won the game for us," Bochy said.

Cain admitted that encountering such trouble forces a pitcher to focus on minimizing the crisis instead of completely halting it.

"Really, you almost have to concede a run -- 'I'll take an out for a run right here' -- and not let it blow up," he said. "Once it keeps going along, you can see how the inning pans out. Being able to get the strikeout, then you can think, all right, let's get out of this without any damage."

Cain considered escaping the first inning unscathed as "kind of an accomplishment." But, he said, "It's also taxing. It kind of messes with the pitch count."

Cain indeed hastened his departure by throwing 30 first-inning pitches. He had unleashed 108 -- including three wild pitches in the fifth inning, the first time a Giants pitcher had done that since 1994 -- by the time he bequeathed a 5-2 advantage to the bullpen. That lead vanished as Upton and LaRoche hit two-run doubles in the seventh inning off Santiago Casilla and Jeremy Affeldt, respectively.

But Guillen, the right fielder who was slow to corral LaRoche's hit, reclaimed the lead for San Francisco by grounding an 0-1 pitch up the middle from Esmerling Vasquez (1-5) after Sanchez singled and Aubrey Huff hit a ground-rule double that left fielder Gerardo Parra lost in the sun. Cody Ross' sacrifice fly accounted for the ultimate stand-up run.

The rest was up to Wilson, who entered the game with a runner on first and San Francisco's lead trimmed to 8-7. Parra, who singled to open the eighth and moved to second base on a sacrifice bunt, sped home as the ball was thrown to first base on Drew's infield single. Parra missed home plate, but catcher Eli Whiteside missed the tag. That enabled Parra to dive back and brush the plate with his left hand for the tally.

Wilson surrendered an odd infield single to Upton, whose chopper trickled up the third-base line, but struck out Kelly Johnson and coaxed Young's fly to center field to end the inning. LaRoche, who went 5-for-14 with eight RBIs in the series, singled to christen the ninth. Wilson responded by coaxing Reynolds' double-play bouncer and Miguel Montero's groundout.

"We need a handful of guys to want to be in those situations to do well," said Wilson, who owns a 0.00 ERA spanning 10 2/3 innings in August. "Today was a situation where I wanted to come in and do my part and shut the door."