If you accept basic physicalism, and you accept science and a naturalized epistemology, then the explanation is that pain is a phenomenological experience that occurs because the physical body needs a control system to ensure survival. Modern biology has documented that congenital insensitivity to pain is a potentially life-threatening disorder of the nervous system.
Clearly, pain is phenomenological which might simply be characterized as that available to conscious experience. Part of the phenomenological is having access to the conscious use of language, and so, it bears worth remembering that explanation is itself linguistic. So, what exactly is one after when one wants to 'explain' pain given the intuitional position of intuitional physicalism or materialism, the idea that we can touch and feel things and there is a difference between the real and imaginary, essentially the primary focus of ontology? Simply, an argument that seems to "make sense" in the face of experience. Science provides a very strong basis for making such a philosophical argument.
For the typical materialist, pain is certainly a real sensation, so the question devolves in various ways depending on your metaphysical presuppositions. Most philosophers acknowledge that there is some sort of connection between mind and body, though the nature of the duality as advanced by Cartesians has been out of fashion for almost 100 years with many contemporary thinkers staking a position closer to Gilbert Ryle and his views on the category mistake.
If your philosophy doesn't reject a naturalized epistemology, then materialism half-way reduces the experience of pain to the tenets of neurology. The reduction is half-way, because modern cognitive science doesn't make causal claims, but rather brandishes the rather undeniable correlations between mind and body and uses the term neural correlates of consciousness. As for the history, the history of neurology can be traced back hundreds of years to the post-Renaissance where scientifically minded thinkers began tinkering with tissues; Galvani is one of the most famous of such thinkers who had an interest in how electricity and the body relate. Today, one of the shinning examples of the triumph of materialism and science is the creation and use of anesthesia which has revolutionized medicine and reduced pain on many fronts. Given my lifelong experiences with pain, anesthesia and analgesics, I'm a firm believer in the relative accuracy of such explanations of pain, and when push comes to shove, most people prefer the benefits of materialism likewise.
So, the proximal answer is that pain is explained by neurological theory, and that theory is built heavily upon difficult-to-refute empirical evidence. You can deny that a physical needle jab and the pain in your hand have nothing to do with each other, but it's rather counterintuitive to suggest so. But, what you're pushing for is something a little more philosophical and is strongly related to Chalmer's hard problem of consciousness (IETP). Which is a question more along the lines of why should the physical body have the experience of pain or consciousness at all? That's a slightly different, but much more difficult question to answer; in fact, no strong consensus exists on whether it's a good question or whether there is a good answer. My sense is that most physicalists accept consciousness as a brute fact.
Many modern physicalists subscribe to the idea that conscious experience, such as pain, is a function of embodied cognition. This is broadly the idea that our consciousness and abstractions are somehow connected or tethered to our physical bodies, a belief not entirely radical to the philosophical intuitions.From WP:
In philosophy, embodied cognition holds that an agent's cognition, rather than being the product of mere (innate) abstract representations of the world, is strongly influenced by aspects of an agent's body beyond the brain itself.4
Of course, some philosophers such as Daniel Dennett go one step farther and call pain and consciousness an 'illusion'; Dennett is probably the most famous proponent of eliminative materialism. But presuming you don't write off pain, the answer might lie in the various scientific models of consciousness posed. For instance, Edelman and Tononi in their book from 2000 proposed a rough answer of sorts in A Universe of Consciousness that in the brain, there is a dynamic core which possess the capacity of neurological reentry such that consciousness is a property of the brain inspecting its own activities. This extends from thinking in evolutionary psychology which attributes survival fitness to cognition.
So, in this way, the conscious mind might be understood as a biological control system which monitors the body through a plethora of subconscious neural computations responsible for maintaining biological homeostasis to further reproductive success. Having pain is obviously a way for afferent signaling to occur from peripheral tissues through the PNS to the CNS preempting many other control loops such as hunger, sexual urges, fear, etc.
As to Why pain? Why feelings? Why consciouness? There may not simply be an answer. It has long been accepted by philosophers that there are brute facts. To wit:
In contemporary philosophy, a brute fact is a fact that cannot be explained in terms of a deeper, more "fundamental" fact.1 There are two main ways to explain something: say what "brought it about", or describe it at a more "fundamental" level. For example, a cat displayed on a computer screen can be explained, more "fundamentally", in terms of certain voltages in bits of metal in the screen, which in turn can be explained, more "fundamentally", in terms of certain subatomic particles moving in a certain manner. If one were to keep explaining the world in this way and reach a point at which no more "deeper" explanations can be given, then one would have found some facts which are brute or inexplicable, in the sense that we cannot give them an ontological explanation. As it might be put, there may exist some things that just are.
Yes, I like many believe, some things just are.