McCutchen broke the game open in the ninth with his 11th homer of the season off reliever Jordan Balazovic, a 438-foot, three-run blast into the second deck in right-center field that gave the Pirates a 7-3 lead.
It was the 298th homer of McCutchen's career and first since June 30. Jason Delay had a double among two hits and two runs, Ke'Bryan Hayes had two hits and an RBI, and Ji Hwan Bae and Liover Peguero each scored two runs for Pittsburgh, which snapped a two-game losing streak.
Keller (10-8) picked up his first victory since June 28. He finished with 12 strikeouts, the fourth double-digit strikeout game of his career and one off his career-high of 13 set May 14 at Baltimore. David Bednar, despite giving up an RBI single to Jorge Polanco in the ninth, picked up his 26th save.
Polanco finished 2-for-3 with two RBIs and a run, Christian Vazquez had two doubles and scored twice, and Michael A. Taylor and Max Kepler each had two hits and an RBI for Minnesota,
Sonny Gray (6-6), despite throwing 5 1/3 perfect innings to start the contest, took the loss, allowing four runs on four hits over 6 2/3 innings. He walked three and struck out seven.
Minnesota took a 1-0 lead in the first inning when Polanco singled, stole second and scored on a comebacker by Kepler that caromed off Keller's foot and into right field for a double.
The Twins extended the lead to 2-0 in the fifth when Vazquez lined a two-out, ground-rule double down the right-field line and scored on a single by Taylor.
After retiring the first 16 batters he faced, Gray was touched for three runs on four hits and walk in the sixth as Pittsburgh took a 3-2 lead.
Peguero broke up the perfect game with an infield single and advanced to second on a throwing error by Polanco. Delay followed with a bloop single and Bae then walked to load the bases. After Bryan Reynolds struck out, McCutchen singled to center to drive in Peguero and Delay. Hayes then gave the Pirates a 3-2 lead with a line single to left to drive in Bae.
Pittsburgh extended its lead to 4-2 in the seventh when Bae sliced a two-out double off the left-field wall to drive in Jack Suwinski, who had walked.
Minnesota cut it to 4-3 in bottom of the seventh when Vazquez led off with a double, went to third on a single by Edouard Julien and scored on a sacrifice fly by Polanco.
—Field Level Media