PHOENIX -- The situation didn't look good for Arizona's already slim playoff hopes.
The Los Angeles Dodgers had won in the afternoon, and the Diamondbacks were facing brilliant young Tim Lincecum, who hadn't lost in two months.
Arizona defied the odds, though, beating Lincecum and the Giants 3-2 on Thursday night on Adam Dunn's two-out RBI single in the eighth inning.
"He's obviously one of, if not the best, pitchers in the league," Dunn said, "and for us to kind of grind and battle and battle and come away with the victory, that's huge."
Conor Jackson singled with two outs and stole second to set up Dunn's game-winning hit as the Diamondbacks swept four games from the Giants to stay 3 1/2 games behind the first-place Dodgers in the NL West with 10 to play. The Dodgers won at Pittsburgh 4-3 in 12 innings.
Lincecum (17-4) was 6-0 in 10 starts since losing to Milwaukee 7-4 on July 20. He allowed three runs and six hits in his second straight complete game and second of his career. The 24-year-old right-hander struck out six and hit a batter in his first loss in six appearances against Arizona.
The Giants are trying to help Lincecum win the Cy Young Award to brighten an otherwise difficult season. The loss came one night after Arizona's Brandon Webb beat San Francisco for his 21st victory.
"It's a sick feeling," Giants manager Bruce Bochy said. "You want the kid to get a win so bad and he pitched his heart out. We're having a tough time getting a hit with men on base. This is probably as tough a one as we've had all year."
The Diamondbacks blew a lead for Randy Johnson for the fifth time this season, again depriving the 44-year-old left-hander of his 295th career victory.
Johnson had left the clubhouse by the time it was opened to reporters after the game.
"RJ matched [Lincecum]," Arizona manager Bob Melvin said, "and in a little different style today. He probably threw more breaking balls than I've ever seen him throw in a game. He felt like he didn't have his best fastball and found a different way to do it."
Johnson gave up a run in the first and scattered eight hits through seven innings, striking out four and walking one. He left with a 2-1 lead, but Bengie Molina homered on his first pitch from reliever Juan Cruz (3-0) in the eighth to tie it.
Chad Qualls threw a perfect ninth for his fifth save in 13 tries.
Eugenio Velez misjudged Justin Upton's line shot to left for a two-run triple with two outs in the sixth to give Arizona a 2-1 lead. Velez took a couple of steps in then saw the ball sail over his head. Stephen Drew started the rally with a double, then took third on Augie Ojeda's sacrifice bunt. Jackson was hit by a pitch, then Dunn struck out to bring Upton to the plate.
Upton somehow got the bat on the ball on a changeup that was barely off the plate.
"It was an unbelievable pitch that he made," Upton said. "He'd been making great pitches all night. I just happened to get the barrel on it."
Lincecum was equally impressed.
"When a guy swings at a pitch like that, you don't expect it to go as far as it did," he said. "I mean, that ball carried. From what I remember looking at it, that ball was practically on the ground and he barreled it up."
Upton's hit ended Lincecum's string of 14 scoreless innings. He was coming off the first complete game shutout of his career, a 7-0 victory at San Diego.
"Everyone's just kind of taking this pretty hard," Lincecum said, "and I think we should."
Lincecum went down in pain when he was hit in his left hand attempting to bunt in the fifth inning. Johnson's pitch was inside and high and it hit the hand where Lincecum was holding the bat.
The San Francisco ace eventually got to his feet and was back on the mound when the Giants took the field.
The Giants loaded the bases with no outs in the first but managed only one run. Randy Winn and Ivan Ochoa opened the game with singles, then Rich Aurilia walked. Winn scored on Pablo Sandoval's one-out sacrifice fly.
Seven Giants singled off Johnson. ... San Francisco plays six of its remaining nine games against the Dodgers. ... Melvin gave 3B Mark Reynolds the night off. Reynolds has taken over the NL lead in strikeouts with 191, one more than Philadelphia's Ryan Howard. ... Arizona last won four straight from July 29-Aug. 1.