Rawle Alkins had 13 points, and his layup off a feed from Deandre Ayton put the Wildcats ahead 69-68 with 37.9 seconds remaining.
Trier made three free throws in the final 17.5 seconds as the Wildcats (16-4, 6-1 Pac-12) won their 16th straight game over Stanford (11-9, 5-2), 15 under coach Sean Miller.
Stanford forward Reid Travis had 20 points and guard Dorian Pickens had all 15 of his points in the second half.
Trier made one of two free throws with 5.0 seconds remaining, but Pickens' off-balance 3-pointer from 35 feet as time expired hit the backboard and kissed the rim but did not fall.
Michael Humphrey had 13 points and Kezie Okpala had 10 for Stanford, which had a five-game winning streak broken.
Pickens had seven points during Stanford's 11-0 run for a 57-46 lead with 9:29 remaining.
Pickens made a 3-pointer to end the run, capping a five-point possession. He made two free throws after Arizona coach Sean Miller was given a technical foul by referee Tony Padilla.
Padilla called an offensive foul on Ayton on the previous possession, and Miller came off the bench and pointed at Padilla while yelling.
Trier, Alkins and Dylan Smith hit 3-pointers as Arizona scored the next 11 points to tie it at 57.
The teams went back and forth the rest of the way.
Ayton, bothered by foul trouble and a Stanford defense that collapsed on him, tied a season low with nine points. He had eight rebounds and a season-high six blocked shots.
Humphrey had 12 rebounds and Travis had 10 as Stanford had a 38-34 rebounding advantage. The Cardinal had 13 second-chance points.
Arizona had a season-high nine blocked shots.
Stanford mixed man-to-man and zone defenses and double-teamed both Ayton and Ristic when either caught the ball in the low post.
Alkins returned after missing the California game Wednesday with foot soreness.
--Field Level Media
Arizona | Stanford | |
Allonzo Trier 21 | Scoring | Reid Travis 20 |
Rawle Alkins 5 | Assists | Daejon Davis 2 |
Dusan Ristic 9 | Rebounds | Michael Humphrey 12 |
Allonzo Trier 7 | Free Throws Made | Dorian Pickens 5 |
Parker Jackson-Cartwright 2 | Steals | Michael Humphrey 2 |
DeAndre Ayton 6 | Blocks | Michael Humphrey 1 |