MIAMI -- The Miami Marlins didn't get a game-winning hit Sunday. But they got a win, and after six straight losses, that was good enough for them.

Miami scored the winning run on a seventh-inning wild pitch, beating the San Francisco Giants 3-2 at Marlins Park.

"We needed to stop this slide that we were on," said Marlins third baseman Casey McGehee, who hit a two-run homer in the first inning to give Miami an early lead. "It was a big win."

Miami (45-52), which hadn't prevailed since July 8, also cooled off the Giants (54-44), who had won three games in a row.

It was the Marlins' 22nd one-run win of the season, which ranks second in the major leagues to the Pittsburgh Pirates, who have 23.

The Marlins' winning rally started with a leadoff double to center by shortstop Adeiny Hechavarria, who advanced to third on a sacrifice bunt by catcher Jeff Mathis.

Giants starter Tim Lincecum (9-6) then bounced a curve ball on a 3-1 pitch to pinch-hitter Reed Johnson. The ball got away from catcher Hector Sanchez, allowing Hechavarria to score from third.

"I felt like Reed was going to get a big hit there -- he was having a great at-bat," Marlins manager Mike Redmond said. "But it worked out that we scored on a wild pitch. We won a tight game against a great team."

Giants manager Bruce Bochy called it a "very tough" way to lose.

"Timmy pitched great," Bochy said. "His stuff was real good -- he struck out his last two hitters.

"He throws a lot of pitches down there (low) -- he's got a lot of movement. He's not easy to catch. Sanchez has done a good job. The ball just kicked away to where (Hechavarria) could score."

The win went to Brad Hand (1-2), who is just 3-12 in his major league career. Hand went a career-high-tying seven innings, allowing six hits, one walk and two runs.

"I was throwing strikes and getting ahead of hitters," Hand said. "I kept the ball low. Hopefully, I can build on this."

Bryan Morris pitched a scoreless eighth, and closer Steve Cishek did the same in the ninth, earning his 21st save of the season.

Lincecum took the loss, allowing five hits, two walks and three runs in seven innings, striking out seven.

The Marlins opened the scoring in the first inning when McGehee drilled his two-out blast to left. It was just the second homer of the season for McGehee, who took advantage of an 0-2 pitch that was up in the strike zone.

McGehee's homer was his first since May 10 at San Diego.

"It was a splitter, but it slipped out of my hand," Lincecum said of his perspiration-plagued pitch to McGehee. "It (stinks) because I had him 0-2."

San Francisco tied the score 2-2 in the third when first baseman Buster Posey, protecting against a strikeout on a 1-2 count, pulled an outside pitch down the line in left for a two-run double. That scored right fielder Hunter Pence and second baseman Marco Scutaro, who had both singled.

But that was all the offense for the Giants, who played backups such as Sanchez, shortstop Joaquin Arias and center fielder Gregor Blanco.

"The back end of our order had a tough day," Bochy said, referring to the final four spots, including the pitcher, that went 0-for-13. "It takes 25 men. We are going to get into some hot weather, the dog days, and we need to keep these (starters) fresh down the stretch."

NOTES: Miami sent INF Ed Lucas to Triple-A New Orleans and recalled RHP Anthony DeSclafani. ... The Marlins said RHP Henderson Alvarez (left-shin contusion) will not miss a start. ... Ex-Marlins OF Jeff Conine, now a special assistant with the club, on Saturday climbed to the top of Mount Kilimanjaro in Tanzania. Conine, 48, has remained physically active since retiring in 2007, competing in marathons, ironman triathlons and racquetball. ... Marlins RHP Brad Penny, 36, allowed just two runs in six innings on Friday at Triple-A New Orleans and may soon return to the majors. ... Giants 1B Brandon Belt (concussion) was placed on the disabled list on Sunday. ... Next up, the Giants open a four-game series at Philadelphia on Monday. ... The Marlins open a four-game series at Atlanta on Monday.