Imported from the Apple Support Forums via the Wayback Machine 🔗
If you have previously synced the iPhone with a computer via iTunes, you can create a new backup as part of the erase process. See the "Erase your device with iTunes to remove your passcode" section of https://support.apple.com/en-us/HT204306
If not, the other possibility would be if you had iCloud Photo Library turned on. In that case, even though you would erase the photos from the phone to remove the passcode, the photos would still be in iCloud and would sync back to the phone after turning iCloud Photo Library on again in the phone. More information: https://support.apple.com/en-us/HT204264