CHAPEL HILL, N.C. — North Carolina went from looking a step slow and struggling to keep Duke's touted freshmen off the boards to roaring out of halftime with an aggressive edge on the way to a rousing win.

Maybe the 21st-ranked Tar Heels just needed their fiercest rival to bring out something that had been missing for weeks.

Joel Berry II scored 21 points and UNC hung on late to beat the ninth-ranked Blue Devils 82-78 on Thursday night, capping an emotional performance that included a comeback from a dozen down in the first half.

The Tar Heels (18-7, 7-5 Atlantic Coast Conference) had recently emerged from their first three-game losing streak in four years by beating the only team (Pittsburgh) yet to win a league game. And they had struggled to find much of a confidence-building flow.

Until Thursday, with the Blue Devils (19-5, 7-4) in the Smith Center in front of a crowd operating at ear-ringing levels.

"Once everybody got going," said Kenny Williams, who matched his career high with 20 points, "we proved to ourselves how good we can be."

The Tar Heels had four players score at least 15 points, including Pittsburgh graduate transfer Cameron Johnson going for 18 points and 13 rebounds in his first taste of the Duke-UNC rivalry. And that helped the Tar Heels reverse a first-half rebounding struggle against Duke's bigger front line of Marvin Bagley III and Wendell Carter Jr., and dominate the glass after halftime.

"Everybody crashed the boards," said senior Theo Pinson, who had seven rebounds. "We told them at halftime: Me and Luke (Maye) are going to do everything we can to keep Carter and Bagley off the boards. You have to come in and rebound.'"

UNC finished with a 28-17 rebounding advantage after halftime that included 15 offensive rebounds, a sign of how the Tar Heels were outworking the Blue Devils as they turned the game out of the break.

"The start of the second half, they played great," Duke coach Mike Krzyzewski said, "and we were awful."

The Tar Heels opened the second half on a 16-2 run to erase what was left of Duke's early lead and go ahead for good. UNC led 61-51 at the end of its run and played from in front the rest of the way.

"We turned a four-point lead into a 10-point deficit," Duke senior Grayson Allen said. "That's hard to come back from against a really good team."

The Tar Heels were tough enough to win despite going just 2 for 18 from the field in the final 8 minutes, while they tied a program record by having just two turnovers for the entire game.

Duke got as close as 74-71 late, and freshman Gary Trent Jr. (team-high 16 points) missed a corner 3-pointer for the tie with 3:33 left. Johnson responded with a huge 3 for the Tar Heels, who got Pinson's dunk off the press break with 11.1 seconds left to all but seal it.