5e doesn't assume PC classes represent NPCs
In some previous versions of D&D, 3e most strongly, player character classes where assumed to be how wizarding (or whatever) worked.
An NPC wizard would use the rules of a level 10 PC Wizard.
4e was a direct backlash against this, in which NPCs where explicitly never built using PC rules. It was listed as an option, but was advised strongly against. A completely different set of rules where used instead, focusing on what you needed to use the NPC as a foe in a single encounter.
5e is agnostic about how you determine monster stats. Instead, it gives you rules on how to take monster stats and turn them into a difficulty number (CR). So in 5e, using PC rules to make a monster is within bounds, but it isn't required.
Monsters built using PC rules tend to have lots of complex moving parts. 5e D&D makes PCs complicated as a way to make players have fun using the complex bits. When a DM is running 5 creatures at once, and those creatures almost all die within a single session, that level of complexity is often not needed.
So, 5e permits you use other ways to build monsters. Many NPCs refer to or are inspired by PC rules instead of following them faithfully. And this is becoming increasingly common as the game progresses.
You can use this as part of the fiction
If you assume PC rules (including XP) are part of how the world works, the logical result is a world full of plucky heroes who go from near commoner to superheroic levels of power within weeks.
If you instead assume PC rules are how you deal with exceptional people who may be destined to become superheroic, and most other people don't suddenly get better at magic by killing goblins in caves, then the result is a very different world.
The PCs meteoric rise to power is now an exception instead of a rule.
PC classes are no longer in-world sets of abilities, but rather specific to the relatively unique hero. Other NPCs might have similar abilities, but need not match them exactly.
Many expert fighters might learn to hit more often. Some holy warriors might have bonded steeds.
But NPCs might learn and gain spells far differently; maybe almost every wizard in the world is actually reading spells off scrolls. This act drains their own power (unlike a PC), so they can only do this a few times per day. And they still destroy the scroll; so they have to do a lot of work to replace it.
In such a world, actually casting a 6th level spell becomes a serious investment for those who can cast it. Meanwhile, for a 13th level PC they are doing it relatively stress free every long rest.
CR measures how hard a fight is
CR doesn't measure NPC level or anything else. With NPCs using different rules for how they are built than PCs, mapping that to the world fiction or to combat capabilities (CR) is the problem.
How the NPC got the abilities? That isn't in the stat block.
One specific pyromancer might be able to cast fireball with a recharge of 56, which no PC can ever do. If you asked how, that pyromancer might be the result of a fire cult sacrificing and breeding mortals until they found a line with extradinary gift for fire magic. Then they embedded living magma within these select souls, turning them into meat-puppets for the elemental prince who they worship in the volcano shrine.
That fiction isn't relevant to the pyromancer's CR. And it is also a deal that few PCs would be interesting in taking. The core part is that the world-fiction doesn't have to be tied to PC mechanics.