So, you've got a plot twist in mind - something that will blow your readers' minds if done well, but you don't want it to seem like you just pulled it out of your hindquarters. How do you go about this?
Back in 2000, I authored Diablo: Demonsbane, the first piece of professional fiction in the Blizzard Entertainment fiction line. At the end of the story was a major twist (I'm not going to worry about spoilers here because the e-book is not only 22 years old, it's also been out of print for years, and is unlikely to be coming back into print anytime soon): for the entire narrative, the main character, Siggard, is dead. A literal walking dead person (not a zombie). Based on the reviews, the reveal at the end managed to take everybody by surprise, and stuck the landing just as it needed to.
So, how do you do it? It's a two step process:
Lay the groundwork for the twist. As strange as it sounds when talking about something that should come as a surprise, you need to do some heavy foreshadowing to make it work. The reason is simple - what decides whether the twist will stick the landing is whether the reader can look back and have the twist make sense. The reveal has to be a natural end-point for where the narrative was leading. In the case of Demonsbane, there were a lot of hints dropped that Siggard's body was not actually normal (he never gets hungry, etc.).
Conceal the groundwork you have laid. The problem with having to lay groundwork is that you now have a bunch of easy-to-read signs pointing to your twist. Now you need to reader to either ignore or misread these signs. Every hint now needs to be paired with a misdirect of some sort. The easiest is a red herring - an explanation offered on the spot that explains the clue at least as well, if not better, than the actual twist. Why doesn't Siggard get hungry? Because right after coming back he discovered that his family was killed, and he's buried in grief at their loss. Once your misdirects are in place, the reader will find themselves looking everywhere but in the direction of the twist that is coming.
And that's all there is to it: lots of clues, each paired with a misdirect of some sort. Do this, and your plot twist will not only surprise the reader, but will stick the landing just as it needs to.