The heat and pain sensors in your hand sent a message to the spinal cord, which sent a message to the arm and hand muscles. That message was something like, “Yikes, get me out of here!”
While all that was going on, a message was on its way to your brain, allowing pain to register after you had already pulled your hand away.