When the phone was in your pocket, the screen rubbed against your body or something else. The screen interpreted that as an incorrect passcode entry.
It might seem unintuitive, but place the phone in your pocket so that screen is facing away from you to help minimize the chances of something rubbing against the screen.
If the problem continues, try the following:
Tap Settings
Tap Accessibility
Tap Touch
Turn off Tap to Wake
This will prevent random touches from waking the screen and you will now need to tap the Side button to wake your iPhone.