VILLANOVA, Pa. — The Maryland women’s lacrosse team gave up a goal 31 seconds into the third overtime, but it wasn’t until the referees checked the stick of North Carolina freshman Sammy Jo Tracy — and the ball fell out easily to signify it was legal — that they raised their arms to show Tracy’s goal had decided the national title game.

The goal in sudden death gave North Carolina a 13-12 victory before 9,391 at Villanova Stadium on Sunday night. It was the longest NCAA title game in the sport’s history.

Tracy’s goal ended Maryland’s undefeated season, its perfect record in NCAA tournament overtime games and its chances for its first national title since 2010. The Terrapins (22-1) also will have to wait to add an 11th NCAA title.

“It definitely stings right now,” said Maryland freshman Taylor Cummings, who lost for the first time since April of her freshman year of high school. “But in the locker room I’ve got 32 great friends. We’ll get through this. We played an amazing game, and I love all my teammates.”

The Tar Heels (18-3) won their first national championship.

Perhaps fitting for a first-time champion, two freshmen made the crucial final plays. Ten seconds into the third overtime, the Terrapins had a fast break, and sophomore Brooke Griffin took a close shot. But North Carolina freshman goalie Megan Ward made the save.

At the other end, junior Brittney Coppa raced past several Maryland players and fed Tracy for a close shot and goal. However, the celebration on the North Carolina sideline didn’t immediately get into full swing. As the players stormed the field and two assistants raced to hug Ward near midfield, the Maryland coaches called for a stick check.

The first problem: Thinking the game was over, Tracy and her teammates had thrown their sticks into the air like students throwing their caps at a graduation ceremony. The game was decided only after Tracy found her stick and the referees checked it and deemed it legal.

“Those were two great teams going after it,” North Carolina Coach Jenny Levy said. “It’s a tough loss for Maryland, but I congratulate them on a brilliant season. It was an amazing game to be part of. After the second or third overtime, I sat back and trusted my kids to do what they do. . . . All these things kept happening. I don’t think I can remember them all.”

Maryland will have no problem remembering. On Sunday night, it trailed at halftime for the first time this season. But North Carolina’s 9-6 lead was gone in less than six minutes, and Maryland took an 11-9 lead on consecutive goals by senior Katie Schwarzmann midway through the second half.

After Schwarzmann’s second goal, there were celebrations on the Maryland sideline, and a backup goalie warmed up on the North Carolina sideline.

Yet Levy stuck with Ward (St. Mary’s-Annapolis), and she responded with four saves in overtime.

The Terrapins tied the game at 12 on a goal by senior Alex Aust on a quick move from behind the goal with 3 minutes 51 seconds to play; Aust gave a stick fake, then shot and scored. After Schwarzmann forced a turnover, Aust had the ball with less than two minutes to play. She raced from behind the goal and went to a move identical to the one she used to tie the game. But her shot went just high.

Still, the Terrapins retained possession and worked the clock for a final shot. The ball went to Schwarzmann with 15 seconds left, but her drive for the goal to win a national title in regulation ended when North Carolina senior Emily Garrity stripped the ball.

The game-within-the-game featured a pair of senior midfielders: Schwarzmann and North Carolina’s Kara Cannizzaro. The pair are two-fifths of the finalists for the Tewaaraton Trophy, given to the best college player in the sport. Schwarzmann finished with three goals and an inches-long gash on her left forearm. Cannizzaro had four goals, two assists and a national championship.

Maryland senior Kasey Howard added five saves, four in overtime.

“We weren’t moving the ball quick enough. [North Carolina] was doing a great job” on defense, Schwarzmann said. “It was awesome to come in as a freshman and play in a national championship game and as a senior to go out doing the same thing.”