The events of Friendship is Magic parts 1 and 2 were staged. Celestia released Nightmare Moon on purpose so Twilight could defeat her using the elements of harmony. The only question is, how did she know there would be a massive wave of villains trying to take over Equestria?