It took 6 hours, 7 minutes to finish, tying 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 in Busch Stadium until the end.
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 Cards 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."
Carlos Beltran put St. Louis in front with a two-run double in the fourth, and also stole three bases, tying a career high.
"Twenty innings or nine innings, a loss is a loss," Beltran said. "We had the opportunities. It just didn't happen."
In other games *
At Houston, the Astros lost in their first game under interim manager Tony DeFrancesco as Aaron Hill homered twice to give the Arizona Diamondbacks an 8-1 win and a sweep. Hill led off a five-run fourth with a shot off Armando Galarraga (0-4) and added another solo homer in the eighth. Hill also doubled.
* At Atlanta, Chad Billingsley (10-9) combined with two relievers for a three-hit shutout to win his sixth straight start, Luis Cruz homered for the second game in a row and the Los Angeles Dodgers beat the Braves, 5-0.
* At Cincinnati, Xavier Paul led off the ninth with a pinch-hit triple and Ryan Hanigan followed with a single, giving the Reds a 5-4 win over the Chicago Cubs.
* At Washington, Bryce Harper had a home run and a triple, and Danny Espinosa homered to lift the Nationals to a 5-2 victory against the New York Mets. Gio Gonzalez (16-6) pitched into the sixth inning, allowing one run and seven hits.
* At Denver, Jordan Pacheco homered among his three hits as the banged-up Colorado Rockies slowed down Giancarlo Stanton and beat the Miami Marlins, 3-2.
Stanton went homerless for the first time in seven games at Coors Field. On Saturday night, he became the first player in big-league history to homer in his first six games in a particular park, the Marlins said in citing the Elias Sports Bureau. Stanton drew two intentional walks and went 0-for-2 in the series finale.
* At San Diego, Clayton Richard (10-12) pitched eight sparkling innings, Cameron Maybin drove in three runs and the Padres snapped a five-game losing streak with a 7-1 win over the San Francisco Giants.