The Twins had runners on second and third before Farmer's one-out single off Kaleb Ort (1-1) ended Boston's six-game winning streak
Max Kepler homered and drove in two runs for Minnesota, which had lost five of its previous six games. Jovani Moran (1-2) pitched a scoreless 10th inning for the win.
Justin Turner homered among his two hits and drove in two runs for Boston, which went 3-for-15 with runners in scoring position.
Boston claimed an early lead in the first against Sonny Gray when Turner drew a two-out walk, stole second and scored on Rafael Devers' double.
Minnesota took its first lead of the series in the second inning on run-scoring singles off Garrett Whitlock by Kepler and Christian Vazquez.
After Turner's solo homer tied the game with two outs in the third inning, Minnesota reclaimed the lead in the bottom half of the inning when Edouard Julien scored from third on Carlos Correa's groundout.
Boston tied the game at 3-3 in the fifth inning when Alex Verdugo extended his hitting streak to 12 games with a one-out triple and scored on Turner's single to center, giving Turner 14 RBIs in his last nine games.
Gray was lifted after giving up three runs on six hits over five innings. He walked two and struck out five while throwing 92 pitches.
Kepler's one-out solo blast off Whitlock in the sixth put the Twins back on top. The homer was Kepler's ninth of the season
Whitlock allowed four runs on eight hits with no walks and six strikeouts over seven innings.
Boston tied the game again in the eighth against Jhoan Duran when Masataka Yoshida drew a one-out walk and was replaced by pinch runner David Hamilton, who stole second and scored on Triston Casas' double.
Boston catcher Reese McGuire exited the game with a right oblique injury on a swing during the sixth inning and was replaced by Connor Wong.
--Field Level Media