Ayton had 13 points and 10 rebounds and Randolph had 13 points as the Wildcats (6-3) won their third straight after falling from No. 2 in the AP poll following three straight losses in the Battle 4 Atlantis tournament two weeks ago.
Forward Tyler Davis had 21 points and guard Duane Wilson had 14 for the Aggies (7-1), who were attempting to start 8-0 for the first time in 10 seasons.
Randolph made 4 of 6 free throws in the final 27 seconds to keep the Wildcats in front, the last time at 65-62 with 5.8 seconds remaining.
Wilson was fouled on a 3-point attempt with 1.7 seconds remaining. He missed the first free throw but made the final two to make it 65-64.
After Arizona's Dylan Smith made two free throws, AM missed a 40-foot 3-pointer at the buzzer.
Dusan Ristic and Smith had 13 points apiece for Arizona. For Smith, it was a season high.
Arizona guard Allonzo Trier, seventh in the nation with a 23.9 scoring average,
had seven points, all in the second half. His first field goal, a 3-pointer with 17:07 left, gave Arizona a 36-33 lead.
Forward DJ Hogg, the Aggies' leading scorer, had nine points and four rebounds.
Arizona made only eight field goals in the second half but converted 20 of 24 free throws after the Aggies got into early foul trouble.
There were nine lead changes in the second half, six in the final 7 1/2 minutes.
Arizona guard Parker Jackson-Cartwright gave Arizona a 61-58 lead on a 3-pointer with 1:54 remaining after Ristic tied the score with a basket on the previous possession.
Hogg made two free throws with 29.5 seconds remaining before Randolph stretched the lead to three again on two free throws with 27.4 seconds left.
Davis made a short baseline jumper to cut Arizona's lead to 63-62 with 17.4 seconds left.
NOTES: Arizona 7-foot-1 freshman F Deandre Ayton, the reigning Pac-12 player of the week, is one of four players in Division I to average 20 points and 10 rebounds a game while shooting at least 60 percent from the field. He was averaging 20.4 points, 11.4 rebounds and shooting 61.4 percent entering the Tuesday's game. ... AM beat Pac-12 contender and then-No. 10 USC 75-69 on Nov. 26. ... Arizona beat Texas AM in Houston last season, weathering a late 18-0 run for a 67-63 victory.