SAN FRANCISCO — Tim Lincecum struck out seven in seven innings and the San Francisco Giants took advantage of a two-run error by shortstop Ruben Tejada to beat the New York Mets 4-1 on Tuesday night and snap a season-long five-game losing streak.

Tejada's throw to first on the back-end of a potential double-play ball by Brandon Crawford in the second inning sailed into New York's dugout fence. The error handed Matt Harvey (1-1) a hard-luck loss and lifted San Francisco's spirits even more on a day the franchise acquired two-time All-Star right fielder Hunter Pence from Philadelphia.

Justin Turner's RBI double in the second was all the Mets managed off Lincecum (5-11). The two-time NL Cy Young Award winner allowed six hits and one walk in another step toward turning around the worst season of his career.

Jeremy Affeldt pitched two scoreless innings for his second save in three chances this season.

After almost a week of bad news, San Francisco's fortunes started to change.

The Giants had not won since slugger Pablo Sandoval strained his left hamstring last week. That landed the All-Star third baseman on the 15-day disabled list while the rival Los Angeles Dodgers — who traded for Shane Victorino from the Phillies and Hanley Ramirez from the Marlins — turned the NL West race into a virtual tie.

The Giants sent Nate Schierholtz and two minor leaguers to Philadelphia for Pence just before the afternoon trade deadline. The Phillies were in Washington, and Pence couldn't to make it to San Francisco in time to be in the lineup.

No need on this night.

Turner doubled off the wall in left to drive home Scott Hairston and give the Mets a 2-1 lead in the second inning. Hairston, whose two home runs powered the Mets past San Francisco 8-7 in 10 innings on Monday night, singled with one out to jumpstart the offense again.

Defense failed New York this time.

After Justin Christian grounded into a force at home plate with the bases loaded and no outs in the second, Brandon Belt grounded into what appeared to be a routine double play. Instead, Tejada's throw overshot first baseman Ike Davis for an error that put San Francisco ahead 2-1.

The Giants loaded the bases again in the sixth and Harvey squeezed out of the jam in similar fashion. Marco Scutaro grounded into a double play to score Ryan Theriot and added a two-out RBI single in the eighth to extend San Francisco's lead to 4-1.

Lincecum, the shaggy-haired right-hander, rallied from a bases-loaded jam in the seventh. He got Josh Thole to line out to second and struck out pinch-hitter David Wright on a wicked curveball that dropped near his knees and froze the All-Star third baseman in the batter's box.

The Giants improved to 7-15 when Lincecum starts. While his miserable summer might not be over, he has showed improvement of late: Lincecum has lasted at least seven innings in three of his four starts since the All-Star break and San Francisco is 3-1 in that stretch.

Harvey gave up four hits, three walks and three runs — two earned — in six innings. He struck out seven, and his 18 strikeouts are the most by any Mets pitcher in his first two career starts. Four others in franchise history had struck out 14.

A few regulars were missing on both sides.

Mets outfielders Jason Bay (stiff left calf) and Andres Torres (strained right thumb) sat out with injuries and third baseman Wright was initially given the night off. Bay pinch-hit in the ninth and grounded out to end the game.

Giants center fielder Angel Pagan sat out with a bruised left hand.

Notes: Mets GM Sandy Alderson decided not to part ways with Hairston or any other players before Tuesday afternoon's trade deadline. "We haven't given up on the season," Alderson said. "We didn't move players off the team for a reason. We think we have lots of good baseball in front of us, and Scott can be part of that." ... Mets closer Frank Francisco (strained left side) struck out all three batters he faced during a rehab assignment for Double-A Binghamton. ... The Giants recalled INF Brett Pill from Triple-A Fresno to replace Schierholtz on the roster. Aubrey Huff is expected to go on the DL when Pence arrives. Huff injured his right knee running to first Monday night. Pence arrived at the ballpark right after the game ended.