Kosuke Fukudome hit a two-run homer for the Cubs, who snapped a four-game skid. Mike Fontenot drove in two runs for Chicago, which got an RBI from Aramis Ramirez.
Ryan Dempster (10-8) went 6 2/3 innings in the start, allowing four runs -- three earned -- on eight hits. The right-hander, who fanned three and walked two, has won five of his last seven decisions.
Buster Posey went 3-for-5 with four RBI for the Giants, who have lost five of seven and sit 2 1/2 games behind the first-place Padres in the NL West. Travis Ishikawa and Pablo Sandoval each drove in a run.
Tim Lincecum (11-6) lasted just four frames, giving up six runs on eight hits. The two-time defending NL Cy Young honoree also struck out four and walked one.
The Cubs scored four runs off Lincecum in the top of the first. Tyler Colvin led off with a single, stole second, and crossed the plate on Castro's base hit. Castro stole second, advanced to third on Micah Hoffpauir's groundout, and raced home on Ramirez's single. Fukudome followed with a two-run homer to right.
San Francisco got a run back in the bottom half. Edgar Renteria singled and Aubrey Huff reached base on Blake DeWitt's fielding error to put men on first and second. Posey's infield single plated Renteria. Pat Burrell, though, grounded into an inning-ending double play.
The Giants got an RBI single from Ishikawa in the second to make it a 4-2 game. Juan Uribe doubled, advanced to third on a passed ball, and scored on Ishikawa's hit to left field.
Chicago plated two runs in the fourth to take a 6-2 lead. Koyie Hill singled, Dempster singled, and Colvin walked to load the bases with two outs. Castro followed with a two-run single up the middle.
San Francisco cut the gap to two again with a pair of runs in the seventh. With two outs, Emmanuel Burrisssingled and Huff walked. Andrew Cashner took over for Dempster on the hill. Posey stepped to the plate and hit a deep fly ball to center. Colvin settled under the ball at the warning track, but the baseball hit off the side of his glove and fell to the ground. Posey was awarded a two-run double on the play, which made it 6-4.
The Cubs, though, got the two runs back in the eighth. Guillermo Mota put men on the corners with two outs. Pinch-hitter Fontenot cleared the bases with a double to right for an 8-4 advantage.
The Giants made things interesting off Carlos Marmol in the ninth. Burriss singled and scored two batters later on Posey's double. Burrell then singled to put men on the corners for Sandoval, who knocked in a run with an infield single. Uribe struck out and Ishikawa grounded out to end the game.
Chicago and San Francisco are meeting for the first time since the Cubs took four of six matchups a year ago, including three of four at AT&T Park...Dempster improved to 4-7 lifetime against the Giants...Lincecum fell to 7-4 in 12 home starts in 2010...San Francisco shortstop Renteria left the game in the fifth inning with a left bicep strain...The Cubs snapped a six-game road skid...Ramirez has 1,000 RBI for his career...Following Monday's game, Chicago recalled infielder Hoffpauir and catcher Welington Castillo from Triple-A Iowa. The club also placed catcher Geovany Soto on the 15-day disabled list with a sprained ligament in his right shoulder and placed infielder Derrek Lee on the bereavement list.