San Francisco scored in the first and sixth innings as the Dodgers, with home advantage, relinquished the leadership. The Dodgers were kept in check by the effective pitching of Madison Bumgarner who gave up just four hits across eight innings.
The reply from the Dodgers, who are now a half-game behind the Giants, came in the ninth inning as Hanley Ramirez cracked a consolation home run off Sergio Romo.
The Washington Nationals tightened their grip on first place in NL East with a 5-4 success over the second-placed Atlanta Braves, pinching victory in the 13th inning as Danny Espinosa scrambled through.
Two home runs from Giancarlo Stanton helped the Miami Marlins to a 12-3 success against the Arizona Diamondbacks in Phoenix, and they were not the only high-scoring team of the night. The Philadelphia Phillies toppled the NL Central-leading Cincinnati Reds 12-5, aided by a two-run homer from John Mayberry Jr.
A pair of home runs from Jonathan Lucroy helped the Milwaukee Brewers see off the Chicago Cubs 9-5, while the Colorado Rockies overcame the New York Mets 3-1 and San Diego rolled past Pittsburgh by the same score.
In the American League, the Texas Rangers strengthened their leadership in the West as they beat the Baltimore Orioles 5-1 with pitcher Ryan Dempster giving up just one run and four hits over eight innings.
Nearest rivals the Oakland Athletics saw their four-game winning run ended by a 7-2 home defeat to the Minnesota Twins.
It took the AL Central-leading Chicago White Sox until the fifth inning to get on the scoreboard, but they then powered to a 9-6 victory over the New York Yankees, Alexei Ramirez striking a decisive two-run homer in the eighth.
The Yankees stay comfortably ahead in AL East, four games ahead of the Tampa Bay Rays who narrowed the gap with a 5-1 win against the Kansas City Royals.
Michael Saunders' pair of two-run homers steered the Seattle Mariners to a 5-3 win over the Cleveland Indians.
Seattle might be at the foot of the AL West, but with six consecutive wins they are the form side in the country.