NEW YORK — In dire need of a break, the San Francisco Giants got one.

Christian Arroyo snapped a ninth-inning tie with a three-run double and the Giants took advantage of a critical error by third baseman Wilmer Flores, rallying past the New York Mets 6-5 on Wednesday to stop a five-game losing streak.

"Yeah, big win," Buster Posey said. "Go home with some momentum and hopefully keep it going."

Hunter Pence hit a tying single off closer Jeurys Familia in a four-run ninth that gave the Giants (12-23) their third victory in 11 games. Posey homered again and scored three runs for San Francisco, which headed home from a 3-6 trip with the worst record in the majors.

Since a comeback win over Miami in May 2015, the Giants had lost 133 straight games when trailing after eight innings — the longest streak in franchise history.

"Today, the ball bounced our way," second baseman Joe Panik said.

Flores nearly tied the score with two outs in the bottom of the ninth, driving a two-run double off the top of the left-center fence for his third hit.

Derek Law, subbing for injured closer Mark Melancon, retired Kevin Plawecki on a dribbler in front of home plate for his second save.

"It's good experience for Law to be in that situation," manager Bruce Bochy said. "But I'll mix it up with the closing."

Left-hander Tommy Milone hit an RBI single and was in position to win his Mets debut before San Francisco fought back.

Panik drew a one-out walk from Familia (1-1), and Eduardo Nunez hit a sharp grounder to third. With a chance to start a game-ending double play, Flores bobbled the ball and rushed a high throw to second base.

"I was just trying to catch it. Once I knocked it down, I didn't put myself in a good position to make a good throw to second base," Flores said. "Just a little mistake cost us the game."

Both runners were safe, and Pence followed with an RBI single through the left side.

After a walk to Posey, the 21-year-old Arroyo cleared the bases with a line drive to left-center and was fired up at second base.

"I think it was just a lot of frustration, a lot of pent-up aggression. It went our way one time," he said.

Familia, booed as he left the mound, was working for the third consecutive day — including a 6-1 victory Tuesday night.

The right-hander had thrown only 15 pitches combined in the previous two outings.

"He's used to coming back-to-back-to-back," Mets manager Terry Collins said. "That was not the issue. He didn't have his command."

Bryan Morris (1-0) tossed 1 1/3 scoreless innings.

Jay Bruce homered and Curtis Granderson hit an early RBI double for the Mets, who led 3-1 before Justin Ruggiano's bases-loaded sacrifice fly for San Francisco in the sixth.

"Those types of wins can turn things around. It's probably just what we needed," Ruggiano said. "Finally, we broke through. I think we all were ready to take a big exhale and smile a little bit."

Giants starter Matt Cain allowed three runs — two earned — in five innings.