If your iPhone is locked and you can't remember the passcode, you can fix the problem by resetting it. This procedure erases all data stored on the device, but if you have a backup file available, you can use it to restore all personal information. There are three ways to restore a locked iPhone: you can use iTunes, the "Find My iPhone" feature, or the recovery mode.
Steps
Method 1 of 3: Using iTunes

Step 1. Connect the iPhone to the computer using the supplied USB data cable
In this case, you will need to use the same computer you synced your iOS device with the first time via iTunes. The latter will be launched automatically as soon as the iPhone is detected.
If iTunes requires you to enter your passcode or if you've never synced your iOS device to your computer using iTunes, follow the steps in step three of the article using recovery mode

Step 2. Wait for iTunes to automatically sync data between iPhone and computer and create a new backup file
If iTunes fails to synchronize the iPhone, select the iPhone icon located in the program window, then press the "Sync" button

Step 3. Press the "Restore iPhone" button when iTunes has finished syncing the data and creating the backup file

Step 4. Choose the "Restore from iTunes Backup" option when the device configuration screen will be shown on the screen

Step 5. Select the iPhone icon within the iTunes window, then choose the most recent backup file from those available
iTunes will restore and unlock the iPhone and then restore all your personal data.
Method 2 of 3: Using the "Find My iPhone" Feature

Step 1. Log in to the iCloud site via the following URL
Do this using any device or computer, then log in using your Apple ID and its security password.
If you have not previously turned on the "Find My iPhone" feature in iCloud, you will not be able to restore your iPhone using this procedure. If this is the case for you, follow the steps described in the third step of the article which involves using the recovery mode

Step 2. Select the "All Devices" item displayed at the top of the iCloud page, then choose your iPhone from the drop-down menu that appeared

Step 3. Select the "Erase iPhone" option
The iCloud service will initialize the device by deleting all content, including the passcode.

Step 4. At this point, choose the option to restore your personal data using an iCloud backup
Alternatively, follow the instructions that will appear on the device screen to go through the initial setup wizard. When finished, the iPhone will be as good as new and fully functional.
Method 3 of 3: Use Recovery Mode

Step 1. Connect the iPhone to the computer using the supplied USB data cable

Step 2. Launch the iTunes program
The latter will take a few moments to detect the iPhone.
If iTunes is not installed on your computer, access the Apple website by clicking this URL to be able to download and install the latest version of the program

Step 3. Press and hold the iPhone's "Sleep / Wake" and "Home" keys simultaneously until the recovery mode screen appears on the screen
The latter will be displayed as soon as the Apple logo disappears from the screen.

Step 4. Press the "Repair" button when iTunes displays a warning message on your computer screen explaining that a problem has been found on the device
iTunes will proceed to download and install all existing software updates, which may take up to 15 minutes to complete.
If the device takes more than 15 minutes to install the latest operating system updates, it is very likely that the recovery mode is no longer active. In this case, repeat steps 3 and 4 of this method before proceeding further

Step 5. Wait for iTunes to complete the iPhone recovery process, then follow the on-screen instructions to run through the initial setup wizard
When finished, the iPhone will be as good as new and fully functional.