Wells had 18 points and 12 rebounds, and Jake Layman added 11 points as the Terrapins (26-5, 14-4 Big Ten) won their seventh straight game.
Shields' three-point play with 43 seconds left pulled the Huskers within one point at 62-61. The Terps called timeout with 11 seconds left and 4 on the shot clock. Wells took the inbound pass and hit his 18-footer from the wing.
Shields missed his last-second 3-point try, giving Maryland a much-needed road win for its NCAA Tournament resume.
Nebraska outscored Maryland 23-21 at the free-throw line, but made only 12 of 22 in the second half.
Maryland went on a 10-0 run to take the lead for good after Nebraska had gone up 48-43. Trimble tied it with a deep 3 and Layman hit another long shot to put the Terps up 53-48.
Shields' jumper and three-point play got the Huskers back to 55-53 and got the crowd on its feet. Layman blocked what would have been the tying shot by Shields, and then Trimble showed off a couple of slick moves in the lane. He snaked through for a layup, and the next possession he passed off to Wells, who made a layup to make it a six-point game with four minutes left.
Nebraska was up six points in the first half after Petteway's dunk of Tai Webster's steal finished a 12-3 run. Trimble was on the bench for most of the time the Huskers were building a lead. He picked up two quick fouls and was limited to nine minutes in the first half.
The Terps chipped away at the deficit, and despite committing nine turnovers and shooting 38 percent in the first half were able to take a 31-29 lead into the locker room.
Maryland: The Terps' 26 wins is a nine-game improvement from last season. ... The 26 wins matches the most in a season in Mark Turgeon's 17 years as a head coach. His 2005-06 Wichita State team, which went to the Sweet 16, also won 26. ... The 7-foot-1 freshman Michal Cekovsky was back after missing two games with a left knee injury.
Nebraska: The eight-game losing streak is the program's longest since a nine-game skid in 2002-03. ... Forward Walter Pitchford returned after missing a game with a hip flexor injury. ... Moses Abraham, Kye Kurkowski, Trevor Menke, David Rivers and Leslee Smith were honored during a pregame seniors night ceremony.
Maryland: plays in a Big Ten tournament quarterfinal Friday.
Nebraska: plays Penn State on Wednesday in the Big Ten tournament.