You have to get it from the original carrier. For a 521 that would be T-Mobile US. They most likely won't provide it for you unless you had their service and left in good standing.
There are 3rd party unlock services that cost money and don't always get the right code. Research any such service before using them to see how they perform with Lumia unlocks and how easy it is to get refunded if it does not work.