How could prophets and exorcists be condemned?

Matthew 7:22-23

Many will say to me on that day, 'Lord, Lord, did we not prophesy in your name, and in your name drive out demons and perform many miracles?' Then I will tell them plainly, 'I never knew you. Away from me, you evildoers!'

Matthew 12:24-27

But when the Pharisees heard this, they said, "It is only by Beelzebub, the prince of demons, that this fellow drives out demons."

Jesus knew their thoughts and said to them, "Every kingdom divided against itself will be ruined, and every city or household divided against itself will not stand. If Satan drives out Satan, he is divided against himself. How then can his kingdom stand? And if I drive out demons by Beelzebub, by whom do your people drive them out? So then, they will be your judges.

How could people who had prophesied in Christ's name and performed exorcists and miracles be condemned at the Last Judgment? Matthew 7:23 seems to say these people were never true followers of Christ, so according to the logic of Matthew 12, how could they have cast out demons if they were neither Christians nor could have been enabled to do it by Satan?

There are several possible answers:

The last answer is feasible, for God can work through nonbelievers and use them to do his will just as he can believers. For instance, Balaam was a sorcerer who ultimately rejected God (Num 24:1), but God still used him as a prophet (Num 23:16, 24:2).