You do this by restoring your iPhone from the last backup – this removes the passcode from your locked iPhone while retaining your data.
Here is how:
1. While your iPhone is still connected to your computer reboot your iPhone by holding down the power and home buttons at the same time. As soon as the Apple logo appears, let go of the power button but keep the home button depressed until you see a Connect to iTunes message appear on the iPhone screen.
2.On your computer, you will see a message appear in iTunes that a phone in recovery mode has been detected. You will be asked to choose whether you want to restore your iPhone to factory settings, or restore it from a backup – if you want to regain all of your personal information, contacts, messages, etc. back on to your phone, be sure to choose to restore it from your last backup.