ST. LOUIS (AP) -- Even Pittsburgh third baseman Pedro Alvarez was starting to lose focus in the later stages of the Pirates' 6-3, 19-inning win over the St. Louis Cardinals on Sunday.
''I was getting a little stagnant out there,'' he said. ''It's one of those things where it's mind over matter. You've just got to fight and keep that desire going.''
Alvarez played a big role in ending the marathon with his 23rd homer of the season. The drive broke a 3-3 tie as the Pirates won the longest game in the majors this season.
It took 6 hours, 7 minutes to finish, trying the longest contest by time, matching a game between Baltimore and Boston on May 6.
Both teams scored a run in the 17th, and each club used eight pitchers.
Alvarez slammed the go-ahead homer off Barret Browning (1-3). Andrew McCutchen added a two-run single later in the inning.
Wandy Rodriguez (8-12), who was set to start for the Pirates on Monday night at San Diego, got the win with two scoreless innings of relief. It was his first win as a reliever in 232 major league appearances.
''That was a backyard tug-of-war,'' Pittsburgh manager Clint Hurdle said. ''We kept playing defense, we kept pitching. At the end of the day, we finally got some separation in the 19th inning.
About 9,000 fans from the original crowd of 43,412 stayed until the end.
Each team had played a marathon in recent years. St. Louis went 20 innings against the Mets on April 17, 2010, and Pittsburgh played 19 innings at Atlanta on July 26, 2011.
The Pirates improved to 4-0 in extra innings this season. They moved two games ahead of St. Louis in the race for the last wild card spot in the NL. The Cardinals have lost three of four overall.
''It was a battle all around on both sides,'' McCutchen said. ''They came to play and we were just able to get the win.
It feels good.''
Alvarez picked on a slider from Browning and drove it 408 feet.
''It felt good out of my hand,'' Browning said. ''It just stayed up. It don't think I've ever been this disappointed. I just didn't do my job.''
Both teams ran out of position players after the 18th inning.
Garrett Jones gave Pittsburgh a short-lived 3-2 lead with a bases-loaded infield single in the 17th. He drove in pitcher James McDonald, who singled as a pinch-hitter, the first of his career.
''It was just luck,'' McDonald said. ''You know how hard pinch hitting is? So it was pretty much luck.''
But St. Louis came right back to tie it on a sacrifice fly by Tony Cruz that brought in pinch-runner Ryan Jackson. Yadier Molina and Skip Schumaker began the 17th inning with singles.
St. Louis left-hander Jaime Garcia, sidelined for 64 days with a strain in his pitching shoulder, struck out a career-high 10 and did not allow an earned run in an eight-inning, 107-pitch stint. He gave up five hits, two unearned runs and did not walk a batter.
Pittsburgh had a pair of hits in the 16th but Molina threw out Jose Tabata trying to steal and reliever Joe Kelly induced McCutchen to ground out to end the inning.
Reliever Edward Mujica got Neil Walker to ground into a double play to end the 11th. Kelly allowed four hits and one run in 5 2-3 innings of his first relief appearance.
Carlos Beltran put St. Louis in front with a two-run double in the fourth, and also stole three bases, tying a career high. Beltran leads the NL with 85 RBIs.
''Twenty innings or nine innings, a loss is a loss,'' Beltran said. ''We had the opportunities. It just didn't happen.''
Pittsburgh starter Jeff Karstens gave up two runs and two hits over seven innings. He struck four and walked one.
The Pirates scored twice in the sixth to tie it on a pair of infield singles and Garcia's throwing error. Clint Barmes reached on an infield hit and Garcia mishandled a bunt attempt by Karstens. Josh Harrison drove in Barmes with a sacrifice fly and McCutchen followed with an infield single.
Chris Resop led the Pirates' bullpen effort with three scoreless innings. Kevin Correia and Jared Hughes added two scoreless innings each.
Hurdle said he has yet to select a starting pitcher to replace Rodriguez on Monday.
''I'll come up with something tomorrow,'' he said.
NOTES: St. Louis 1B Allen Craig extended his hitting streak to nine games with a single in the fourth inning. .. Jon Jay snapped an 0-for-14 skid with a two-out single in the 13th. .... Garcia's 10 strikeouts are the most by a St. Louis left-hander since Mark Mulder had 12 on May 9, 2005, against Los Angeles. ... The Cardinals sent RHP Trevor Rosenthal to Triple-A Memphis prior to the game to make room for Garcia. .... St. Louis plays host to Houston in a three-game set beginning Tuesday. Adam Wainwright (11-10, 3.87) will face Lucas Harrell (10-8, 3.81) in the series opener. Wainwright drew a walk as a pinch hitter Sunday. ... Pittsburgh closes out a six-game road trip with three at San Diego starting Monday. ... Pirates 2B Neil Walker did not start for the fourth successive game with a dislocated pinky on his right hand. He later pinch hit. ... Molina grounded out to second to finish a 14-pitch at-bat in the fourth.