The Giants used four pitchers to survive a messy final inning and won their fourth straight, moving three games in front of the Los Angeles Dodgers in the National League West.
Zito (10-8) allowed only three hits and struck out five through eight innings, and went to the mound in the ninth in hopes of completing a shutout.
Affeldt promptly gave up a two-run double to Freddie Freeman and walked Dan Uggla.
San Francisco built a 5-0 lead with its four runs in the fifth, the last two coming in on a double by Sanchez.
The loss went to starter Tommy Hanson (12-6), who allowed all five runs on seven hits in 4 1/3 innings.
McPhee, Cokas 'working on their marriage' after affair
Duggar sisters unveil Christian dating rules in new book