The Pirates won their third in a row and stayed within a half game of the NL Central-leading Cincinnati Reds.
Pittsburgh overcame a two-run deficit in the fourth and then went in front on Walker's eighth homer of the season, which he hit off Ricky Nolasco as the leadoff batter.
Jose Reyes hit a solo shot for the Marlins in the first.
Hanrahan put two runners on with one away before striking out both Reyes and Emilio Bonifacio to end the game.
Nolasco (8-8) gave up all four runs on eight hits in six innings.