Hamilton also had a three-run homer for the Rangers, who scored the winning run after a wild ninth to split the four-game series.
Delino DeShields drew a leadoff walk in the ninth off Nick Goody (0-1), who was making his major league debut. Yankees closer Andrew Miller got Elvis Andrus out on a deep fly ball before DeShields became the second out when he was hit on the foot by Leonys Martin's hard shot while running the bases.
Martin went to second on Adrian Beltre's walk and scored on Hamilton's sharp single to right.
Mark Teixeira homered twice for the Yankees, and Brian McCann also went deep.
It was 101 degrees at the start of the game, the hottest this season in Texas. Even as a full moon rose over the field, the temperature was still in the low 90s when the game ended.
The Rangers played the night before the non-waiver trade deadline while waiting for the completion of an expected deal to acquire ace left-hander Cole Hamels and a reliever from Philadelphia for Matt Harrison and a package of top prospects.
Rangers officials said during the game that there would be no announcement Thursday night.
New York completed a trade before Thursday's game, acquiring utility player Dustin Ackley from Seattle for right-hander Jose Ramirez and outfielder Ramon Flores.
Teixeira has 26 homers this season after the 40th multihomer game of his career, and third this season. But he struck out in the ninth against Shawn Tolleson (3-2), the Texas closer pitching in a non-save situation.
After a solo homer to center in the first, Teixeira hit another in the seventh to put the Yankees up 6-5.
McCann's two-run shot in the third made it 5-4 after Teixeira reached on a single.
Texas had three homers, including Ryan Rua's inside-the-parker that tied the game 5-all in the fourth.
Rua hit a sinking liner that bounced over the glove of diving center fielder Jacoby Ellsbury and rolled to the wall.
Hamilton had a three-run homer in the first, after New York had scored three times in the top of the inning. Shin-Soo Choo led off the second with his 13th homer of the season for a 4-3 lead.
Yovani Gallardo, the Rangers' right-hander who has been mentioned as a possible trade target for other contending teams, allowed five runs over six innings. He has given up 15 runs over 14 innings in his last three starts, after an earlier stretch this season with 33 1-3 consecutive scoreless innings over a span of six games.
CC Sabathia gave up five runs — all scoring on the three homers — while pitching through the leadoff batter in the sixth. He has allowed 24 homers, most in the American League.
Expected Yankees starter Michael Pineda was put on the 15-day disabled list earlier Thursday because of a right flexor forearm muscle strain. The move was retroactive to July 25, the day after Pineda's last start.
Yankees: An MRI on Pineda's forearm showed no structural damage. When asked where he felt pain, he pointed to a spot a few inches below his right elbow. ... RHP Ivan Nova said he has talked with other pitchers who have undergone Tommy John surgery, like his in April 2014, and was told the arm fatigue he felt in his five innings Tuesday at Texas is normal. Nova expects to make his next scheduled start Sunday at the White Sox.
Rangers: LHP Derek Holland threw 39 pitches in 1 2-3 innings for Triple-A Round Rock in the first start of his injury rehab. He strained his shoulder in the first inning of the home opener April 10.
Yankees: New York on Friday night opens a three-game series against the Chicago White Sox. That is the final series in the Yankees' 10-game road trip that matches the season's longest.
Rangers: For the first time since the 2010 World Series, the Rangers host the San Francisco Giants. The Giants played at Texas for MLB's first-ever interleague game on June 12, 1997, but haven't played a regular-season game there since 2001.