Farrakhan, playing in front of his grandfather, Nation of Islam Minister Louis Farrakhan, for the first time in his college career, nailed a 3-pointer from the top of the key with 3:23 left to give the Cavaliers a commanding nine-point lead.
N.C. State never got closer than seven the rest of the way.
Will Sherrill, who tied a career high with four 3-pointers, added 14 points for Virginia (15-14, 6-9 Atlantic Coast Conference).
Sherrill hit two 3-pointers as the Cavaliers stormed to a 16-0 lead. The Wolfpack (15-14, 5-10) responded with a 17-2 spurt, including seven points from Javier Gonzalez, who finished with 16.
Virginia led 32-28 at the break, then opened the second half with a 10-3 run, capped by another Sherrill 3.
"We can lose a lead quicker than most teams and it doesn't seem like we can make up a deficit quicker than most teams," Virginia coach Tony Bennett said. "Part of that is decision-making, part of that is inexperience. But we withstood and pushed ahead and were able to make enough sound decisions to come away."
Bennett was happy that his team was able to send Farrakhan and Sherrill, his two seniors, out on a good note. They were playing their last regular-season home game.
"I told (them) at the shoot-around that I was very proud of them," Bennett said. "(I said) the way you can give back to your family is to play your guts out.
"Don't get your heads in the clouds or be floating around. Enjoy your little moment (before the game), but then get locked into the game because that's what this team needs. I thought they both did that well."
Virginia concludes its regular season Saturday on the road at Maryland while N.C. State finishes up at home against Florida State on Sunday.
"They played well, they were active and they were aggressive," N.C. State coach Sidney Lowe said. "I knew this was going to be a difficult ballgame. They're tough here."