Minnesota seemed on it way to win with a 6-3 lead in the ninth but Brandon Barnes extended the contest with a two-out, three-homer off Twins closer Glen Perkins.
The game stayed knotted until the 12th when Thomas got aboard with one out and went to third on a double by Josmil Pinto (4-for-5, two runs scored).
Then, with Ryan Doumit at the plate, Houston's Kevin Chapman (0-1) uncorked a wild pitch, allowing Thomas to come home with the go-ahead run.
Mastroianni (3-for-6) later added a two-run double as Minnesota clinched its three-game series.
Trevor Plouffe supplied four hits, including a two-run homer, and Brian Dozier also went deep for the Twins.
Caleb Thielbar (3-2) notched the decision with two shutout innings and Jose Roenicke picked up his first season of the season.
Barnes finished 3-for-4 with five RBI and Chris Carter homered for the Astros.