Allegany's Georghe DeGroat's tip-in to give the Trojans an 80-79 with 47 seconds remaining as part of a tremendous game with 26 points (13-for-19 shooting) and 17 rebounds in just under 30 minutes.
Matheny drew a foul on the ensuing possession and calmly hit both foul shots to provide the winning points.
"I wasn't nervous. We practice foul shooting a lot," said Matheny, a Martinsburg (W.Va.) graduate. "I just wanted to make a contribution, get us the win."
HCC tightened its defense in the final seconds as Allegany worked for the final shot. Calvin Lee fired a long jumper that banged off the rim at the buzzer.
"It was a great game. This rivalry is something else," Devlin said. "We split with them during the regular season and we may see them again in the regionals.
"I like playing Allegany. They are always out there hustling, battling and it takes a great effort to beat them. A game like this can only help us down the road."
HCC guard Kelvin Bright scored 22 of his 26 points in the first half while Matheny added 13 to give HCC a 52-42 lead at the half.
But it was short-lived.
Allegany dominated the boards in the first 10 minutes of the second half to grab a 64-63 lead at the 11:56 mark.
The Trojans increased the margin to 73-64 before HCC got rolling again and grabbed a 74-73 lead on Anthony Perez' jumper with 5:57 left.
The teams exchanged baskets the rest of the way.
Allegany enjoyed a 48-30 rebounding advantage but HCC hit seven 3-point baskets to none for Allegany.
The free throw line also proved the Trojans' downfall, hitting just 10-of-18 foul shots, compared to the 16 of 23 for HCC.
"We pulled through. It was a matter of determination," Devlin said. "It was a tough game and we found a way to win."