Sanchez's fly ball to deep right field came after Dodgers pitchers J.P. Howell (1-3) and Jamey Wright loaded the bases with walks. Angel Pagan, who drew a leadoff walk against Howell, scored on Sanchez's fly off Wright.
Brandon Belt followed with an RBI single through the left side to make it 3-1 and Sergio Romo stranded a runner at second in the bottom of the 10th to pick up his 11th save as the Giants improved to 5-2 against their NL West rivals this season.
The Dodgers played at home for the first time since April 27 after going 5-4 on a road trip that took them through Minneapolis, Miami and Washington.
Josh Beckett was eight outs away from his first win in more than 19 months before Brandon Hicks tied the game for San Francisco with a solo homer to left field in the seventh inning to make it 1-1.
In his sixth start of the season, the Dodgers right-hander gave up five hits and three walks in 6 2/3 innings, striking out four.
The former AL Cy Young runner-up hasn't won since Sept. 30, 2012, his last start that season, after he came over in a trade from Boston.
Giants righty Ryan Vogelsong allowed one run on five hits and two walks in 7 1/3 innings.
Hunter Pence had the game's first hit, a one-out single off Beckett in the third inning. LA's first hit was Yasiel Puig's single with one down in the fourth.
Matt Kemp led off with a single in the fifth, advanced to third base after Carl Crawford's walk and Juan Uribe's fly out and scored when Drew Butera lifted a sac fly to center field to give the Dodgers a lead.
Adrian Gonzalez grounded into a rally-killing double play for LA with runners at the corners in the sixth, and Hicks belted his sixth homer of the season in the top of the seventh to tie the score at 1-1.
Beckett was taken out after giving up a two-out double to Pagan and Chris Withrow got Pence on a line drive to end the inning.
Beckett, who is 0-1 with a 2.80 ERA, had his second good outing against the Giants after throwing five shutout innings against them on April 15, a 3-2 Dodgers loss in 12 innings.
UPI horse racing roundup