I think the problem with proximity sensor. It’s damaged or it installed in wrong way. please check your sensors. Also check in settings “pocket mode” or something like that. Probably it’s in display settings or somewhere in accesibility settings. If you turn off “pocket mode” you will not face again “screen locking itself” problem.
I thinkyour device have malware from web browser. If you hard reset problem will never gone. You have to update your firmware with Computer. The malware called "Monkey Virus". You can remove malware with monkey virus remove tool. Just type monkey virus in Google for more details and removing tutorials.