Ash and Sam free Celebi from the Iron Masked-Marauder's mind control, however, it dies soon afterwards. As all hope seems lost, a portal opens from which many other Celebi emerge, and revive their fallen friend.
The Iron Masked-Marauder makes a final attempt to kidnap Celebi but is taken out for good by Pikachu. Celebi returns Sam to his own time.
It is revealed at the very end that Sam was actually Professor Oak as a child.
Thanks Mark I!