The Terrapins (21-5, 9-4 Big Ten), the nation's No. 2 team in free throws made with 447, sank 21-of-26 and were 9-of-19 from three-point range.
Penn State (15-11, 3-10) was led by 25 points from D.J. Newbill, who reached double figures for the 100th time in his career. Geno Thorpe added 17 points and Brandon Taylor had 11 for the Nittany Lions, who have lost four of five.
Maryland's win broke a three-game losing streak in conference road games.
The Terps led 72-70 with a minute remaining when they committed their 11th turnover. Newbill then slipped and lost control of the ball, and Trimble hit a pair of free throws.
Newbill recovered with a long three-pointer to bring the Lions to 74-73 with 13.6 seconds left.
Trimble was fouled on the inbounds pass and put the Terps up by three. Newbill took a 25-foot shot that bounced high off the rim and then off the top of the backboard to end the game.
Wells gave Maryland its first lead on the Terps' opening possession of the second half. He scored seven of Maryland's 11 points in the opening 3 minutes to put the Terps up 44-39.
Jake Layman's three-pointer and a layup by Damonte Dodd stretched Maryland's lead to 49-41. Jared Nickens' back-to-back 3s pushed the Terps to a 55-42 advantage during a 6-minute, 22-8 run Maryland produced to open the half.
Three points from Newbill and two points each from Shep Garner and Jordan Dickerson reduced the Terps' lead to 55-49.
Layman and Wells countered with five straight, but an 11-0 run with 3s from Garner and Brandon Taylor knotted the score at 60 with 8:57 left.
The pace of the game slowed with multiple fouls called. But Layman blocked Thorpe's fast-break layup attempt and sank a short jumper of his own to put Maryland up 70-64 before Newbill's 3-pointer brought the Lions within three at the 2:09 mark.
Richaud Pack's second three-pointer of the first half cut Penn State's lead to 33-30, and a three-point play by Wells, who had nine first-half points, tied the game at 33. Ross Travis' free throw with a second remaining put the Lions ahead, 34-33, at halftime.
Maryland returns to the friendly confines of the XFINITY Center for a game against Nebraska on Thursday at 7 p.m.