What is most remarkable about these two verses is the very feature which makes the verb ὁμολογέω homologeo to stand out in its real meaning in this particular place, for the concept of the word is much broader than we might otherwise ever be aware of and had the OP not posed the question, one might never have considered the word's breadth of scope.
Usually, in the KJV at least, 'confess' is quite adequate and, indeed, preferable, to cover the concept in context. But here, in these particular two verses, Jesus' words are so remarkable that 'confess' is, I would say, so far off-centre from the concept's broad spectrum of meaning that it is actually a poor translation.
Strong (3670) gives the basic, etymological, meaning of 'to speak the same, to agree'. Further down the page is the meaning 'to assent' or 'to acknowledge'.
... the LORD seeth not as man seeth; for man looketh on the outward appearance, but the LORD looketh on the heart ...
So speaks the prophet Samuel, I Samuel 16:7. And here, I think, is seen just how deeply the Lord looks.
Who would ever think that one who casts out demons and does wonderful works in the name of the Lord Jesus, would be anything but welcomed - and that most heartily - through the gates of pearl ?
But the Lord has already said (in 7:20) :
by their fruits ye shall know them.
So the deeds they did demonstrate something. They demonstrate a state of heart that only the Lord can see. Since he alone knows 'I never knew you', he alone knows that their deeds (however marvelous they may appear outwardly) cannot, possibly, be done ...
... for His sake.
Because they do not know him !
Therefore, these deeds, done in the name (only) of the Lord Jesus, are not done within a relationship in which he is involved.
But all their works they do for to be seen of men - Matthew 23:5 (KJV 1769)
It is all selfish. It is all outward. It is all for men to see. It is all spectacular - but pointless.
The Lord speaks in alignment to, in acknowledgment of and in assent according to - their state of heart, not their outward appearance.
They are condemned not for any other thing. No other misdeeds or sins are spoken. Just this one thing - that the Lord never knew them.
No repentance, no faith, no personal relationship.
It is all iniquity . . .
. . . depart from me.