Lincecum (15-7) walked two batters and struck out seven.
He increased his strikeout total to 261, six short of Christy Mathewson's 1903 franchise record.
Andres Torres led San Francisco with three hits -- one a solo fifth-inning homer -- and two RBI.
Pablo Sandoval had two hits and one RBI.
Ryan Rohlinger and Bengie Molina hit RBI doubles.
Rich Aurilia had one RBI.
A run scored on Justin Upton's fourth-inning throwing error, while Randy Winn and Eugenio Velez added to hits each for the Giants, who have won four straight to keep their slim National League wild-card hopes alive.
Upton's two-run ninth-inning single led Arizona.
Stephen Drew hit an RBI single for the Diamondbacks, who have lost five of seven.
The losing pitcher was starter Dan Haren (14-10).
Chipotle plans first price increase in 3 years
Putin thinks Obama would save him if he were drowning