If you have Find My iPhone enabled then you will need to enter the account's password to log out of it - factory resetting the phone will not remove the phone from the account, after doing so you will have to enter the id and its password to reactivate the phone.
If you can't remember its password then you should be able to reset the password via http://iforgot.apple.com, either by answering the account's security questions or via email.
If you can't do it via iforgot then try contacting Support in your country and see if they can reset the password : Contact Apple for help with Apple ID account security - Apple Support
If your country isn't on that page then try this form to contact Support : https://www.apple.com/emea/support/itunes/contact.html