A compulsion is the response to an obsession. The thought produces anxiety, the ritual lowers it, and the lowering strengthens the ritual. The loop is called negative reinforcement and is one of the most powerful learning mechanisms the brain has.
The problem is that the ritual also supplies a false explanation: nothing bad happened because I checked. So the person never finds out that nothing would have happened anyway, and the belief stays untested indefinitely.
Many compulsions are invisible. Checking the cooker is visible; mentally replaying a conversation, counting, silent prayer, seeking reassurance from friends or online — is not. Anyone describing their problem only through visible rituals usually underestimates how much time it takes.
The best-evidenced treatment is exposure with response prevention: deliberate contact with what triggers the anxiety, without performing the ritual. It is uncomfortable by construction, it is done gradually, and it works because it breaks precisely the link that sustains the problem.