Bob burns his hand on the stove. The pain makes him jerk his hand away. Luckily for him, if his hand had stayed on the heat, it would have been deeply burned. The next day, Bob is still in pain, but there is no jerking away left to do. That first jolt was enough.
A thief tries to steal Joe’s car. The alarm goes off and the thief runs away. The alarm stops blaring. Why keep sounding all night when the purpose is passed?
The difference between these two alarm systems is simple. The first evolved over thousands of years. Pain as an alarm system evolved because those that didn’t have it (or didn’t react to it) didn’t survive. The main gain is not getting killed, there is no immediate advantage in turning down the volume.
The second alarm system was intelligently designed. We thought ahead to get it to work better.
Why the pain? If we had been designed by a perfect all-knowing being, couldn’t he have done better?