Updated

Matt Cain dominated for eight innings.

The San Francisco Giants were barely hanging on at the end.

Cain threw five-hit ball to snap his three-game losing streak, but the Giants narrowly avoided another loss to Atlanta in the final at-bat, preserving a 7-5 victory over the Braves on Wednesday night.

After Cain turned over a 7-1 lead, the Braves scored four runs and had the tying run at the plate. Brian McCann struck out swinging against Jeremy Affeldt.

"I think we won," manager Bruce Bochy quipped. "Just when you think you have a chance to cross your legs for a second, we're facing the tying run up there."

The Giants broke it open with four runs in the fourth, one of them driven in by Cain (10-9) on a sacrifice fly. But he did his best work on the mound, striking out nine and giving up only an unearned run in the first on Chipper Jones' bases-loaded walk.

Cain started working out of the stretch exclusively, even while retiring the last 18 hitters he faced.

"Mechanically, in the windup, I wasn't quite going right," he said. "So I switched to the stretch to try to keep stuff a little more in line and found that good rhythm and just tried to stay with it."

San Francisco pounded All-Star Jair Jurrjens (12-5), taking advantage of a pitcher who wasn't sharp in his first start coming off the disabled list. He surrendered eight hits and five runs in six innings.

"I felt good," Jurrjens insisted. "I just left some pitches up. I was a little rusty."

The Braves won the first two games of the series, rallying for three runs in the ninth for a 5-4 victory Monday, then pulling out a 2-1 win in 11 innings on Tuesday. They nearly did it again, closing to 7-5 on Martin Prado's two-run double after Giants shortstop Orlando Cabrera dropped Michael Bourn's soft blooper behind the mound to extend the inning.

Affeldt got McCann on a 3-2 pitch.

"You don't want to go down without a fight," Atlanta's Freddie Freeman said. "At least we gave them a little scare at the end."

Once the Giants pushed their margin above three runs, Atlanta was in trouble. San Francisco improved to 32-0 when leading by at least three runs, the majors' only undefeated team in those situations.

The win came at a good time for the struggling Giants, who've been plagued by injuries and were knocked out of first in the NL West with a stretch of only five wins in 18 games. They closed to 2½ games behind Arizona in the division race and with five games of wild card-leading Atlanta.

The Giants broke open a 1-all game in the fourth. Aubrey Huff led off with a double, Nate Schierholtz singled and Cabrera brought home the go-ahead run with the third hit in a row. Brandon Belt walked to load the bases, and Eli Whiteside pushed the lead to 3-1 with another run-scoring single.

Back-to-back drives to the warning track made it 5-1. Cain hit a sacrifice fly to deep center, and Cody Ross followed with a liner that was caught in front of the left-field wall, allowing Belt to trot home.

Jurrjens wasn't exactly fooling the Giants even when his teammates caught the ball. Schierholtz hit one to the track in right that was hauled in by Jose Constanza.

San Francisco closer Brian Wilson wasn't available to pitch after flying to Florida to have his ailing right elbow checked out. It's nothing serious, but Bochy gave Wilson a second night off to rest his arm.

The way Cain pitched, it looked like the Giants would be just fine without the bearded one.

Then came the ninth.

"We've had a couple heartbreakers at the end the last two nights here," Cain said. "It just seemed like everything accumulated. It was really nice to get everybody relaxed and go out there and have fun, so we can just carry that over into the rest of the week."

San Francisco jumped ahead in the first on back-to-back doubles by Mike Fontenot and Pablo Sandoval. The Braves tied it up in the bottom half, taking advantage of a miscue by Sandoval at third base.

With two runners on and one out, Sandoval fielded Dan Uggla's grounder and attempted to tag Bourn running toward third. Bourn dodged the glove, and Sandoval's throw to first was too late to get Uggla, either. It was the first of three errors by the Giants.

Cain struck out Freeman with the bases loaded before Jones walked to bring home the tying run.

Jurrjens was pitching for the first time since Aug. 1, having gone on the disabled list because of a strained right knee. He hasn't been the same dominating pitcher he was before making the All-Star team, giving up at least four runs for the fourth time in his last five starts. That happened only once in his first 16 appearances.

"Right now, I just need to go to the bullpen and get my pitches back," Jurrjens said. "I've got to get a feel for them again."

The Giants added to their lead in the ninth with two more sacrifice flies off Scott Linebrink. Sandoval picked up his second RBI and Huff brought home the other run.

NOTES: Braves RHP Peter Moylan is scheduled for another bullpen session Thursday as he continues his recovery from back surgery. ... Whiteside left the game in the seventh because of dizziness. He smashed his face into the dirt when thrown out attempting a steal to end the fourth. ... The Giants tied a San Francisco record with four sacrifice flies, last done in 1988. ... The Giants beat the Braves for the first time in six meetings this season. ... The series finale pits San Francisco's Tim Lincecum (11-9) against Atlanta's Mike Minor (2-2). Lincecum is 6-3 in nine career appearances against the Braves, while Minor will be facing the Giants for the first time.