If you have synced your iPhone with iTunes before you forget your iPhone password, you can reset your iPhone by using iTunes without losing data. Through iTunes, you can restore your iPhone from the last backup and remove the passcode from your locked iPhone while retaining your data. If iTunes asks you to enter the passcode, try another computer. Otherwise, you have to follow the reset iPhone Passcode with Recovery Mode section below. The following steps will show you how to reset iPhone password with iTunes.
Connect your iPhone to the computer with via a USB cable that your iPhone has trusted.
Launch iTunes. If you've synced your iPhone with iTunes, then no "enter password" is required.
iTunes will automatically sync your iPhone and create a backup file. If it doesn't, you should sync your iTunes and create a backup file manually by in the Summary window.
In the Summary window, you can see the option "Restore iPhone". Click it and follow prompts to choose the latest iTunes backup to restore your iPhone.
After the restoration process is complete, your passcode will be erased and all your data will be restored from your backup. You will now be able to access your iPhone without any passcode. And you can set your new password on iPhone.
For more methods, check: How to Reset Password on iPhone