The Pirates had grabbed a 3-2 lead in the top of the 17th on a run-scoring infield hit by Garrett Jones, but the Cardinals came right back in the next half-inning to knot it up again on sacrifice fly by Tony Cruz.
Pittsburgh, however, made sure the contest would end in the 19th when Alvarez pounced on a pitch from Barret Browning (1-3) and put it into the left-field seats for a 4-3 lead.
The Pirates then loaded the bases with two outs and Andrew McCutchen drove in a pair of insurance runs with a single to right.
McCutchen finished with three RBI for Pittsburgh, which won a 19-inning contest for the first time since 1979 and took the rubber match of a three-game series.
Wandy Rodriguez (8-12) -- the Bucs' eighth pitcher -- collected the victory as the Pirates retained their hold on one of the National League's two wild card spots.
Carlos Beltran drove in a pair for St. Louis, which fell two games behind the Pirates in the wild card chase.