Deus ex machina
Definition: This refers to the incidence where an unlikely concept or character is brought into the story, generally abruptly, in order to make the conflict in the story resolve. This device is not commonly used because it is a mark of a poor plot that the writer needs to resort to random and unbelievable twists to try to end the story. Authors can use it for comedic purposes to make it so that the deus ex machina surprises the audience yet it was already the solution being set up all along.
Literary example: Oliver Twist by Charles Dickens
Let the tears which fell, and the broken words which were exchanged in the long close embrace between the orphans, be sacred. A father, sister, and mother, were gained, and lost, in that one moment. Joy and grief were mingled in the cup; but there were no bitter tears: for even grief itself arose so softened, and clothed in such sweet and tender recollections, that it became a solemn pleasure, and lost all character of pain.
Explanation: Unexpectedly, at the end of the book it turns out that Oliver’s fellow orphan Rose is actually his aunt, though neither had any idea. This is a huge intended plot twist because it was being foreshadowed all throughout the story.