>"Congratulations!"
"The ID No. of the <winning Pokémon's nickname> in your PC Box matches your Loto Ticket number!/The ID No. of your team's <winning Pokémon's nickname> matches your Loto Ticket number!"
This is from Bulbapedia. It says 'your' Pokemon. So if you don't have the Pokemon, you can't win the lottery with that ID no., unless you have another Pokemon with the same ID no.
So if you trade/release the Pokemon, the game no longer holds the ID no.(unless you have another mon with the same ID)
The game does not save ID numbers.