The blast by Werth, coming at the end of a 13-pitch at-bat against Lance Lynn, evened the National League Division Series at 2-2.
A series winner will be determined on Friday in Washington with the survivor taking on San Francisco in the NL Championship Series. The Giants advanced earlier Thursday with a 6-4, Game 5 victory over Cincinnati.
After winning the first game of the series, Washington suffered back-to-back losses by a combined score of 20-4.
The Nationals pulled out Thursday's game despite producing a mere three hits -- two of them homers. Adam LaRoche hit a home run in the second for Washington, his second of the series.
San Francisco also had only three hits and scored its run in the third, an unearned one, on a sacrifice fly by Carlos Beltran.
Ross Detwiler started for the Nationals and gave up all three hits over six innings. Drew Storen picked up the win by striking out two in the ninth.
Kyle Lohse gave up a run on two hits in seven innings during his start for the defending World Series champion Cardinals.
Werth, who hit five home runs during the regular season, was the leadoff batter in the ninth. He looked at two strikes to begin the at-bat, then worked the count to 2-2 before fouling off six consecutive pitches.
After taking a ball that sent the count full, Werth fouled off another pitch before hitting a shot into the Washington bull pen.
The victory was the first playoff win at home for a Washington franchise in 79 years -- that being a 4-0 decision for the Washington Senators over the New York Giants in Game 3 of the 1933 World Series.
Friday's game will bring back the pitchers who faced each other in the series opener -- Gio Gonzalez for Washington and Adam Wainwright for the Cardinals. Washington rallied to win that game 3-2.
Couple mistakenly served bag of cash at McDonald's drive-thru
Benedict Cumberbatch's dramatic reading of R. Kelly lyrics is just what you need