Samardzija (1-4) allowed six hits and walked none in seven-plus innings. He also had an RBI double during a three-run fourth to help snap his 16-start winless streak.
The right-hander entered with six no-decisions in his first 10 outings this year despite leading the majors with a 1.46 ERA.
Pablo Sandoval homered and drove in three runs for the Giants, who had won four in a row. Sandoval has five home runs and 12 RBIs in his last eight games.
Yusmeiro Petit (3-2) gave up four runs and six hits in five innings while filling in for injured starter Matt Cain, who was scratched after exiting his last outing with a strained right hamstring. Petit struck out five and walked none.
Left-hander David Huff allowed four runs and seven hits in 1 1-3 innings of relief as the Cubs provided plenty of runs to back Samardzija's long-sought victory.
Chicago had lost nine of Samardzija's first 10 starts. He had not won since Aug. 24 at San Diego last season, even though he has allowed more than two runs only twice this year, including Monday.
Cubs manager Rick Renteria pulled Samardzija after Angel Pagan singled leading off the eighth. It was the sixth time in Samardzija's career he had at least 10 strikeouts.
That Samardzija's skid ended at San Francisco - which entered with the best record in the majors - only made it that much sweeter.
The Giants' hottest hitter capitalized on two of the few mistakes Samardzija made. After Pagan reached when Samardzija dropped the ball for an error while covering first base, Sandoval singled with two outs in the first to give the Giants a 1-0 lead.
The Cubs came back in the third on a solo shot by Schierholtz, a former Giants outfielder. Sandoval put San Francisco up 3-1 with a golf-like swing on a pitch that looked well below the strike zone in the fourth.
Samardzija hit a tying, two-out double and scored on Emilio Bonifacio's triple during a three-run fifth that put the Cubs in front 4-3.
Brandon Crawford's RBI single in the seventh accounted for San Francisco's final run.
NOTES: The Cubs activated OF Justin Ruggiano from the 15-day disabled list and optioned OF Ryan Kalish to Triple-A Iowa. ... Giants manager Bruce Bochy said Cain is progressing well and the team hopes the right-hander will take his next turn in the rotation Saturday at St. Louis. ... Tim Hudson (4-2, 2.13 ERA) takes the mound for the Giants opposite Chicago's Jake Arrieta (1-0, 2.33) on Tuesday.