The Maryland men’s soccer team hit the left post and the crossbar, the right post twice and had a shot cleared off the goal line.

Virginia’s Will Bates made sure his late bid didn’t meet a similar end.

Bates’s supreme strike in the 84th minute Friday provided a 2-1 victory that ended the second-ranked Terrapins’ unbeaten streak at 12.

The Cavaliers (7-4, 3-1 in the ACC) endured Maryland’s close calls before turning to Bates, who smashed a 17-yard one-timer into the top right corner in front of 2,898 at Klockner Stadium.

“They don’t come too often,” Bates, a junior forward, said of the stunning effort that produced his 10th goal of the year. “Put anything on frame and give it a chance.”

The goal capped a crackling match between teams meeting for the 74th time in their decorated histories. Virginia freshman Ryan Zinkhan scored in the 51st minute before senior Casey Townsend tied it in the 61st with his 12th goal of the season.

Maryland (11-1-1, 3-1) hit the left post in the 33rd minute, the crossbar in the 52nd and the right post in the 55th and 79th – both by Townsend. Virginia struck the crossbar late in the match.

“We deserved better,” said Terrapins Coach Sasho Cirovski, whose team was enjoying its best start to the regular season since 1968. “It’s one of those games where you are just a little off. You start to think it might end up this way.”

The Cavaliers field more talent than their record indicates, with pro prospects Bates, Hunter Jumper and Brian Span, a sophomore attacker who trained with two European clubs over the summer.

Virginia seized the early initiative, but as the match unfolded, the Terrapins found their bearings and began to test freshman goalkeeper Spencer LaCivita. In the 33rd minute, Taylor Kemp curled a 30-yard free kick off the left post.

Three minutes later Townsend surged into the clear before pushing a 17-yard bid a whisker wide of the right post – an uncharacteristic off-touch by one of the nation’s top scorers.

Maryland threatened twice more at the start of the second half, but Felipe Libreros cleared Patrick Mullins’s header off the goal line and LaCivita made a leaping touch save on Townsend’s chip from the top of the box.

The frantic pace turned in Virginia’s favor in the 51st minute as Bates’s diagonal ball linked with Zinkhan for an angled one-timer to the far side of the net.

Maryland was undeterred, even after Mullins struck the crossbar and Townsend clanked the right post in a three-minute span. In the 61st, Townsend turned at the corner of the six-yard box and tagged a low shot past LaCivita.

Townsend continued to menace the Cavaliers, and in the 79th, directed a header off the right post. Bates confronted no such obstacles in the late moments. Zinkhan laid off Kyler Sullivan’s cross to Bates, who struck the ball with power and spin beyond goalkeeper Will Swaim’s reach.

“It’s world-class. You hear that term all the time, but side-footed from that range to the top corner at that time of the game?” Cavaliers Coach George Gelnovatch said. “It was a very good sequence, a very good goal.”

The Terrapins played their fifth game in 14 days, and despite the abundance of opportunities, showed signs of fatigue defensively. They conceded multiple goals for just the second time this season.

The Cavaliers have won three of four since consecutive home losses dumped them out of the coaches’ poll.

“This is a huge win for us,” Bates said. “Hopefully we get back on the map with this one.”