Deus ex machina

Deus ex machina is a plot device used mostly at the end of a story to resolve situations that would otherwise render the story unresolved, though through an element that is out of place with the rest of the narrative. The term, which means "god from the machine", comes from its use in Greek theatrical productions where a godlike character would be manually inserted into the narrative to resolve situations going on in the play by the end.