Dempster (10-4), one of eight Cubs selected for Tuesday's game at Yankee Stadium, entered with a 10-0 record in 11 home starts this season, but was upstaged by another brilliant start by Lincecum (11-2). The Giants right-hander struck out nine, allowed one runs and six hits and also drove in a run with his first career triple.
Ray Durham hit a two-run single in the third and Fred Lewis tripled in a run in the seventh for San Francisco, which ended a six-game losing streak.
Brian Wilson survived a shaky ninth inning to record his National League-leading 25th save. The closer yielded an RBI single to fellow All-Star Geovany Soto but bounced back to strike out pinch-hitter Daryle Ward and coax Ryan Theriot into a game-ending groundout with a runner on third.
The Cubs finished a 4-2 homestand and head into the All-Star break with a 37-12 record at Wrigley Field.
The bottom of the Giants order hurt Dempster in the third inning. With one out, Omar Vizquel broke an 0-for-11 skid with a single to center and stole second. Lincecum followed with a hard liner to right fielder Mark DeRosa, who charged the ball but couldn't get in front of it. Lincecum's drive rolled all the way to the wall, easily scoring Vizquel.
Durham added a two-out, two-run single later in the inning to give the Giants a 3-0 lead.
Dempster (10-4) allowed four runs and seven hits in 6 2-3 innings. He struck out six and walked two.
Before the game, Carlos Marmol became the eighth Cubs player on the NL All-Star team when the reliever was selected to replace injured teammate Kerry Wood. Chicago is the fourth team in NL history to send eight players to the game.
Before this year, the most All-Stars for the Cubs was six in 1936 and 1988. Outfielder Alfonso Soriano (broken hand), who was voted to the starting lineup, will miss the game because of a broken hand.
Notes: Wood has a blister on his right index finger. He pitched on Friday, closing out a 3-1 win over the Giants for his 24th save in 29 opportunities. Wood hopes to return Friday. ... Dempster is 0-7 as a starter against the Giants.