Dimension Door would allow exit from the Mental Prison without harm.
Mental Prison states
[...] the target takes 5d10 psychic damage, and you make the
area immediately around the target's space appear dangerous to it in
some way. You might cause the target to perceive itself as being
surrounded by fire, floating razors, or hideous maws filled with
dripping teeth. [...]
[...] If the target is moved out of the illusion, makes a melee attack
through it, or reaches any part of its body through it,[...]
o que significa que the target would only perceive that immediate area as dangerous. If the spell had not used the word 'immediately', then it would be clear that everything outside the target's space would 'appear dangerous to it'. However, since that word was used in conjunction with 'to it', then it can be reasonably argued that everything beyond the immediate area não appear dangerous to the target.
Further, of the three examples for being harmed after the illusion is up, 2 clearly state interacting with said illusion as the source of harm (by use of the word 'through'), leaving the third example, being 'moved'.
The question is, does the use of Dimension Door count as being moved?
Dimension Door states
You teleport yourself from your current location to any other spot
within range. You arrive at exactly the spot desired. It can be a
place you can see, one you can visualize, or one you can describe by
stating distance and direction, such as "200 feet straight downward"
or "upward to the northwest at a 45-degree angle, 300 feet."
So, Dimension Door allows you to disappear from one point, and appear in another, without even knowing anything about the destination (so being unable to see beyond your space as a result of Mental Prison would not prevent the use of this spell) by way of teleportation.
If we look at the rules on Opportunity Attacks:
You also don't provoke an opportunity attack when you teleport or
when someone or something moves you without using your movement,
action, or reaction.
we see that being teleported is explicitly distinct from being moved. So using Dimension Door would not count as being 'moved', nor would it cause any part of the target to reach 'through' the illusion.
This means that Dimension Door would allow the target to exit the illusion of Mental Prison without harm.