Gray became Texas' first 100-yard rusher on the season and Texas' defense, pushed around in two consecutive losses, put up a solid and energetic performance, forcing three critical second-half turnovers.
Ash, who missed the previous game with concussion-like symptoms, passed for 166 yards in the first half but missed the second with a new head injury. Senior Case McCoy took over, leading two touchdown drives.
Texas coach Mack Brown has been under intense pressure after three sub-par seasons and a bad start this year. The victory allowed Texas (2-2, 1-0) to avoid its first 1-3 start since 1956.
John Hubert scored two touchdowns and Tyler Lockett had 13 catches for 237 yards for Kansas State (2-2, 0-1).
The Longhorns snapped a five-game losing streak in the series, beating the Wildcats for the first time since 2003.
Gray's first touchdown in the second quarter came after he made two dazzling cuts on a 21-yard run. His second put Texas up 24-7 in the third. Malcolm Brown ran for Texas' last touchdown after the Wildcats had pulled within 10 points early in the fourth quarter.
Texas came in as a team desperate to turn around it season. After consecutive losses, Brown urged his team to focus on the Big 12 and trying to win the league crown. The Longhorns, most notably the defense, responded with their most complete game of the season against the defending Big 12 champs.
The defense, which played so poorly in the two losses, gave up a huge night receiving to Lockett, but forced the three turnovers that swung momentum early in the second half and preserved the win late.
The victory could ease some of the pressure on Brown, even if just for a few hours or days.
Brown has been harshly criticized by Texas fans after the bad start to the season and his job future beyond this season rapidly escalated into a matter of open speculation.
On Thursday, a member of the Texas board of regents acknowledged that he and a former regent spoke in January with Alabama coach Nick Saban's agent about Saban coaching the Longhorns if Brown retired. Two days later, Brown was asked if he would consider retiring, but the matter was dropped when Brown said he wanted to keep coaching.
Saban dismissed speculation he'd be interested in the Texas job, but that didn't stop some Texas fans from printing burnt-orange "Saban 2014" t-shirts and wearing them to the game Saturday night.
Ash, who had been cleared to play Friday night, showed no effects of the previous head injury early in the game. But he took several big hits, both on runs and when standing in the pocket, before floating a perfectly-thrown pass to Kendall Sanders for Texas' first touchdown.
Texas went up 17-0 after Ash converted a fourth down with a short pass to Jaxon Shipley to set up Gray's first touchdown. Kansas State responded with its first drive across midfield and Hubert spun through two tacklers on a 15-yard touchdown run with 1 minute left in the half.
Ash was removed at the game at halftime to be evaluated for a head injury and didn't return, putting the game and the job of managing the lead in McCoy's hands. Kansas State fumbled on the first possession of the third quarter and McCoy handed the ball to Gray seven times in eight plays on a 56-yard drive to a touchdown and a 24-7 lead.
Hubert's second touchdown pulled Kansas State within 24-14 early in the fourth. Kansas State had pulled within 10 and was driving for a touchdown when Waters, who ran for a touchdown and passed for 275 yards, fumbled on the Texas 6 with 2:03 left to play.
The Wildcats had one last chance when Tramaine Thompson returned a punt 40 yards inside the Texas 15, but Waters was sacked twice and fumbled with 17 seconds left.