In the great majority of cases, any phone you've unlocked from a carrier's network will remain free from restrictions after you perform a hard reset. As such, you can go ahead and return your device to its factory settings as per your mobile OS user guide. A small number of devices may re-lock in error after a hard reset, so it's wise to have your original unlock code to hand in case you experience any problems.
Locate the code you used to unlock your device. This may have been sent to you via email, post or text message. If you have problems finding it, contact the network provider that issued your code and ask for it to be resent. This won't be necessary if you're trying to unlock an iPhone, as unlock requests go through Apple and are applied via iTunes.
Backup any files and data you want to keep from your handset to a computer. All your personal information will be lost during the restore process. Check your device's user guide to find out how to transfer content from your phone to a PC. Most modern smartphones will allow you to backup over the air, but be conscious of data usage if you decide to go down this route.
Find the restore instructions for your handset in its user guide or online. Apple, Google, Microsoft and BlackBerry all supply factory reset instructions for their mobile operating systems on their websites.
Perform a factory reset, go through your device's setup process and transfer any files and data you want back on your phone from your PC. Once you've done so, switch SIMs to make sure your device is unlocked. If it isn't, you'll need to enter your unlock code to lift network carrier restrictions. You should have been sent instructions on how to use your unlock code when you received it. If you don't have these, consult your device's user guide or contact the network provider that issued your code for assistance.