Ann,
Sure, but you have to first apply the code that AT&T sends you by following their instructions precisely. If the code does not work you need to contact them.
How to unlock your iPhone for use with another carrier - Apple Support
Then you have to do the Restore. It looks scary but in reality iTunes walks your through it rather easily. Again you just have to follow the instructions slowly and precisely. Here is a summary:
1. You will have to disable Find my iPhone first. Don't forget to set it back to On when you are through (iTunes should do this automatically but check any way and set Send Last Location to ON).
2. You will be connecting your iPhone to your computer. Then do a full backup to iTunes, just follow the instructions below. Then select Restore iPhone and iTunes will install a fresh copy of the current iOS and will erase all your data. After iTunes has installed a fresh copy of the iOS, it will ask you if you want to set your device as New or restore from Backup. Choose From Backup. Restoring will bring back all your Settings, Apps, Photos on Camera Roll, etc. You should not lose anything.
3. After completion check to see if all works well. I have done this myself many times and restored from backup with no difficulties - it takes 30-60 minutes.
Use iTunes to restore your iPhone, iPad, or iPod to factory settings - Apple Support