The Giants moved to within six games of the first-place Dodgers and snapped their three-game losing streak to the Rockies.
"Unlucky 13, right?" Lincecum joked. "Me and [Matt] Cain have been working on that for a while. ... Oh my God, it was painful. It was a long, long drawn-out process, but I'll take it."
Cain will have to wait until the series finale Sunday against Colorado for his next shot to win his 13th game.
The calendar says it's still August, but Friday night's game had a definite postseason feel to it, and Lincecum turned in an effort worthy of October.
"He's such a good competitor," Giants manager Bruce Bochy said. "He knew how big the game was. He pitched his heart out."
Lincecum, matched in a pitching duel with Colorado's Ubaldo Jimenez, threw a season-high 127 pitches before giving way to closer Brian Wilson in the ninth.
Wilson gave up a leadoff single to Todd Helton, but he struck out Troy Tulowitzki, got Brad Hawpe on a deep fly to center then fanned Chris Iannetta to end the game and set off a celebration on the field and in the stands.
"This win is huge," Wilson said. "You want to get that Game 1, then go for tomorrow, then the sweep. Obviously it's tough to sweep a team, but right now is the time to do it, and if we can do that, we can just get a little momentum going."
Sandoval put the Giants ahead, 1-0, in the fifth inning when he launched a two-out, opposite-field home run into the left-field stands off Jimenez.
Sandoval left Monday night's game against the Rockies in Denver after the top of the third inning with a strained right calf. He walked as a pinch-hitter Tuesday night but missed games Wednesday and Thursday because of the flu and his sore calf.
In the fifth inning, Sandoval put a great swing on a nasty curve and broke up a scoreless game with his team-leading 20th home run of the season.
"I just tried to put the ball in play," said Sandoval, who received a standing ovation as he circled the bases. "I put a little backspin on the ball."
More importantly, Sandoval put a little jolt into the Giants.
"The guy's unbelievable," Wilson said.
In the sixth, the Giants made it 2-0 when catcher Eli Whiteside walked, moved to second on Lincecum's sacrifice bunt then scored when Eugenio Velez lined a single to right.
Lincecum set the tone early, striking out the first two Rockies he faced. He fanned Eric Young with a fastball then Seth Smith, who hit a two-run bomb against him Sunday in Colorado, with an offspeed pitch. He struck out two more Rockies in the second and had seven through six innings.
The Rockies, though, made Lincecum work. They had at least one runner on base every inning from the second through the sixth, but Lincecum got out of every jam, with a little help from catcher Eli Whiteside.
Whiteside, subbing for the injured Bengie Molina and catching Lincecum for the first time, gunned down Tulowitzki twice trying to steal second, once in the second and again in the fourth.
Whiteside definitely saved a run in the fourth with his strike to second base. After he nailed Tulowitzki, Brad Hawpe followed with a double down the right-field line. Hawpe was stranded when Iannetta flew out to right.
"Eli saved my butt a couple times," Lincecum said.
The Giants knew they were in for a battle against the hard-throwing Jimenez. On Sunday at Coors Field, he held them to two runs and six hits over eight innings, striking out nine. He carried a six-game winning streak into Friday night's game and a streak of nine straight games in which he allowed three or fewer runs. He was 5-0 with a 1.63 ERA in five August starts.
This time, Jimenez took the loss, and the Rockies lost for the third straight time.
"We're human," Jimenez said. "We were facing one of the greatest pitchers out there, so it's going to be hard. We have to find a way. We have lost three in a row, but it's part of the game."
On Monday, the Giants suffered a brutal 6-4 loss in 14 innings to the Rockies, falling four games back in the Wild Card chase. This time, they were on the right side of a two-game swing.
"It's big," Bochy said of the win. "That's a big swing tonight."