SAN FRANCISCO -- The San Francisco Giants have raced to the best record in the majors with few contributions from two of their biggest stars.
Posey hit a tiebreaking two-run homer in the eighth inning for his first long ball in more than a month, Cain made a successful return from the disabled list and the Giants won for the eighth time in 10 games by beating the New York Mets 4-2.
"He's a talented hitter," manager Bruce Bochy said of Posey. "We all have our ups and downs. We talk about that all the time. He saw the ball well tonight."
The big blast off Carlos Torres (2-3) capped a three-hit day for Posey, who entered the game with only four hits in his past 42 at-bats at home and no homers since May 3.
Angel Pagan started the rally with a leadoff walk against Torres and advanced to second on a groundout. Posey followed with a long drive to left-center to snap a 93 at-bat homer drought as the Giants (40-21) became the first team in the majors to reach 40 wins.
"In that situation you're just trying to get something you can stay in the middle of the field with and try to get that run in from second," Posey said. "Fortunately I got a good pitch I could handle and it went out."
Daniel Murphy hit a two-run homer for the Mets, who lost their fourth straight game. The bullpen has taken all four of the losses and has a major league-worst 15 losses on the season.
"He was the perfect guy," manager Terry Collins said of Torres. "He's had great numbers against the Giants. He doesn't hardly walk anybody. Buster's made a career of getting big hits."
Cain allowed two runs and three hits in seven innings in his first start back from a stint on the disabled list for a strained right hamstring. But he got a no-decision and still has only one win in nine starts in a rough season that has included two trips to the disabled list.
"We had the fastball working pretty well today," Cain said. "We were on both sides of the plate and tried to mix some off-speed stuff after that. The big key today was getting the fastball over for strikes."
Cain was in complete control with a 1-0 lead having faced the minimum 18 batters through six innings before the Mets quickly turned the tide in the seventh.
Matt den Dekker led off the inning with a double for New York's second hit of the game and Murphy followed with his fourth home run of the season to put the Mets ahead 2-1.
The Giants tied it in the bottom half when Brandon Hicks and Brandon Crawford teamed up again to produce a run. Hicks drew a two-out walk from Jonathon Niese, advanced to second on a wild pitch and scored on Crawford's single to center.
The duo produced San Francisco's first run in the fifth when Hicks tripled and scored on Crawford's sacrifice fly to make it 1-0.
Niese allowed two runs and five hits in seven innings, extending his streak of consecutive starts allowing three earned runs or fewer to 16 but still ended up with a no-decision.
"It's tough," he said. "You go out there and battle and you want to see the win beside your name. But it is what it is, it's part of the game. It'll come eventually. I have all the confidence in the offense we have here. They're going to put up runs."
There was a pregame moment of silence for Don Zimmer, who coached for San Francisco and played for the Mets. Zimmer died Wednesday at age 83. ... Bartolo Colon (5-5) starts Saturday for the Mets against Tim Hudson (6-2).