An evil warlock has killed all but two of the party. He then proceeded to trap the party wizard, and cast Magic Jar on her to trap her in a sacred jewel that the other party member, a paladin, is honour-bound to preserve.

There's a large issue with this though: warlocks can't cast Magic Jar.

Not only is this warlock a warlock, they are a level 20 warlock. They can't, for example, have 19 levels in warlock and 1 in wizard, then have used a scroll to cast Magic Jar.

Anything else about the warlock is changeable. They can have any number of Epic Boons, any race, etc.

3 Answers
3

This Warlock knows how to cast Magic Jar

That's it. That's all it takes. This guy can just do that.

In 5e, NPCs don't need to follow Player Class restrictions or guidelines. Dragons don't have Sorcerer levels, but can do magic. The Master Thief doesn't have Rogue levels but gets Sneak Attack and Uncanny Dodge. Lich gets 18th-level Wizard Spellcasting but doesn't need a spellbook or arcane focus and has a +7 proficiency (not even level 20 wizards get that).

NPCs are not bound by character rules.

So what if your NPC is a Warlock? You're the DM, and you can create whatever you want. You can create a custom enemy that resembles very closely a Warlock, and that is also able to cast that spell, no explanations required. As to why he can do it, it could be a number of reasons, some more RaW than others.

Some that come to mind...

[RaW] Warlock's pal cast a Wish for him to know it

Warlock's patron has favored him with this spell

Special magical item that allows him to cast it once (like KorvinStarmast's Artifact)

Warlock has 20 Warlock levels and 1 Wizard level, and is using a scroll

Warlock has 20 Warlock Levels and 12 Wizard Levels

Please remember, NPCs are built differently from PCs. They are meant to have a single encounter, against a party of players. Players are meant to work in groups and have several encounters every day. Sure, you can model your villains after classes, but there is no need to bind them to actual rules. Bend them a bit, give your villains cooler things to do, more damage, less HP, and, in this case, a cool new spell!

\$\begingroup\$As someone who loves (trying to) homebrewing, I like the special patron idea. As a player I would love to see the kind of unique patrons the GM could come up with.\$\endgroup\$
– 3C273May 28 '18 at 23:22

The Warlock may have an Artifact

On page 219 of the DMG, there is a Major Beneficial Property on the Artifact table that allows the artifact's possessor to cast a sixth level spell. (Entry roll 71-80). There are no class restrictions on the spell stated in that description. The property includes chances for being able to cast a 4th, 5th, 6th or 7th level spell. It is a fairly powerful feature, since that spell may end up being cast more than once ...

Once cast, roll on a d6; on a 1-5 the spell cannot be cast again until the next dawn. (DMG, p. 219)

A level 20 character having an artifact is not beyond the limits of the game.

Beyond that, the DM can provide that spell because the DM in D&D 5e is master of rules. (DMG, p. 5; also the topic of Part III of the DMG). While none of the warlock NPC characters in Volo's Guide to Monsters are shown as having that spell, the DM can include that spell for any caster who could cast 6th level (or above) spells. Two other answers address this point as well.

Modifying a Class Spell list is addressed in the DMG

Changing Spell Lists (DMG, p 287) addresses this case directly.

Modifying a class's spell list usually has little effect on the
character's power but can change the flavor of a class significantly.
... you could build a new paladin spell list with spells meant to
protect their masters, drawn from the sorcerer or wizard lists. Be cautious when changing the warlock's spell list. Since warlocks regain their spell slots after a short rest, they have the potential to use certain spells more times in a day than other classes.

Given that magic jar is a 6th level spell, the limitation imposed by mystic arcanum (cast once per day; PHB, p. 108) would be a sensible choice by the DM in this case.

While the NPC spell casters typically remain within the same spell list as a given class (p. 342, MM) even when spell swapping is decided, the customization of an NPC to include spells from other lists is similar to how a bard can use spells from other lists via Magic Secrets, how a Cleric's Domain spells often come from other spell lists, or how a Warlock's Pact of the Tome ability provides (ritual) spells from other classes. It's not a huge reach to customize an NPC with spells from another spell list. The Changing Spell Lists section (DMG, p. 287) addresses this directly.