In the beginning people were dumb and believed in spooks. They weren't actually dumb, but many (all?) early cultures believed that certain inanimate objects had spirits. Most attributed spirits to the sun and moon; some were more liberal and believed rocks and other mundane things had spirits. With the march of the progress of Science, the set of things which most people ascribed spirituality to contracted. Angels fell out of favor, the devil was an increasingly unsatisfying answer, even god no longer existed literally in, or just beyond, the sky.
But exactly which things are sppoks? Clearly literal spooks, spooky ghosts, are spooks. What about angels? Probably. Miracles? I guess. The human mind? Well, wait a minute. As far as we know it's not physical, at least not in a very simple and obvious way. The idea of a square, which I may contemplate right now, is not located anywhere. Not the idea of it, even if instances of squares exist in places.
But Science abhors a non-physical thing we believe in. The behaviorists in Psychology therefore believed they were advancing the march of Science by trying to identify the mind with behaviors of people--physical things that could be observed by scientists, measured, tested, and so on. Pain for them was a tendency to wince, groan, seek relief, and so on. Pain was identical to a set of behaviors.
This obviously wouldn't do, though. One can behave that way without pain, and one can have pain without behaving that way. Enter the functionalists who would like to persist in identifying the mental with something physical. Rather than say that pain was identical with behavior, they claimed that pain and other mental states were identical with a function.
An analogy from one of the greatest American philosophers, David Lewis, was that of a lock. Common sense tells us that the lock will unlock when the numbers on its pad are correctly aligned. This is the fairly superficial understanding of what the lock is. We may later learn through close observation and scientific discovery that in fact, the alignment of numbers coincides with the alignment of certain gaps in metal plates inside of the lock, such that when they are aligned the bolt in the lock can pass through, and unlock the lock. The lock is defined by its function, and the unlocking mechanism is that which plays the causal role of causing the lock to unlock. It turns out that this phrase superficially denotes the alignment of numbers on the pad, and more deeply denotes the alignment of gaps in metal plates.
So by analogy, our superficial understanding of pain is the mental experience of it. However, pain just is that which plays the causal role whereby crushed or torn flesh causes a certain set of behaviors like wincing. We know it by the mental experience, but since certain physiological or neurological functions coincide with this and cause the experience of pain, then these must be the same thing. To keep things easy, let's imagine that humans had a single pain sensation caused by something called C-fibers. A person is in pain, we imagine, if and only if that person's C-fibers are firing. Since this plays the right causal role, with inputs of some kind of physical damage, and outputs of pain-like behavior, it is identified with the mental experience of pain.