If the universe were a closed system composed of nothing but temporally "closed" subsystems, then the eventual entropic decay of each "isolated" system -- and of the universe itself -- would be inevitable. However, for some currently unknown reason, at some point in the past, a process called life emerged on at least one planet in the universe, effectively breaking the previously prevailing chain of tendency toward system disorder. Once this event took place, it became possible for systems that were more or less closed in the traditionally understood, macroscopic sense to -- almost contradictorily -- remain open -- by seeking out energy actively rather than passively.
Of course, to prevent integration with competing systems and other environmental parameters, these new systems also had to remain partially closed -- at least in the sense of putting defense and regulatory mechanisms in place. This kept the systems well-defined, with physical barriers and spatial limitations, while still allowing them enough openness to acquire the energy necessary for their perpetuation.
So what did this opening of otherwise self-contained, self-regulating systems accomplish? For starters, it introduced homeostasis into the environment. This was initially a radical, if meaningless, departure from the way in which energy had been transferred from one location to another in the past. After the fact of life's emergence, though, it turned out that the "mostly closed" systems, or organisms, were nevertheless quite susceptible to the various forces of the universe, and thus, entropy. Mobility and a binary attraction-repulsion system enabled them to disperse energy in an entirely new way, but other physical agents were still quite persistent in their vying for physical space, and were occasionally successful at bringing about states of maximum disorder among some living organisms; this eventually culminated in what we call death.
The organic instructions to resist disorder were mutually persistent, however; as time went on, organisms managed to find, by happenstance, new ways to perpetuate themselves -- even with both living and non-living "space competitors" vying for the same resources. Finally, a few billion years into this routine, one motivation mechanism of incredible efficacy arose -- sentience.
But why does it matter? What was sentience effective at?
Well, as it would turn out, the goal of sentience was not to help organisms "enjoy" their processes; rather, it was to stop organisms from decaying, as it had been with all previous biological mechanisms.
In short, this meant that neural nets would go on to continually birth, over and over again in successive generations, increasingly complex incentives for organisms to avoid behaviors and parameters conducive to their own destruction. At some point during all of this, fully robust brains emerged, and with them, not the capacity to feel pain, but pain itself. To put it succinctly, brains did not attempt to manage pain to the benefit of organisms; they attempted to manage disorder to the benefit of nothing, using the pain that they created entirely on their own as a motivator. We sneeze a lot when sick not because viruses convert themselves into mucus as they multiply, but because, to prevent the body from being destroyed, the immune system must produce mucus. We experience pain not because external agents are inherently painful, but because brains are painful while attempting to prevent disorder.
If you're not following along, again, "disorder" in this context refers to that lack of physical work that causes closed systems to literally "freeze," having no more energy to convert from one state to another; everything has been evenly distributed, and each piece is incapable of transferring energy to any other, or has itself decayed.
Is there anything wrong with decay, though? When two weather patterns collide and eventually disperse their energy content, leaving no further work to be done, is this a bad result? Is it something to be avoided, or even stopped at all costs? It doesn't appear to be, based on anything that we've ever observed; furthermore, without any good reason to invest in the god hypothesis, the agenda of brains (and of central nervous sytems as wholes) must be questioned, for the alternative to the god hypothesis is that the universe -- and thus, all constituents, including central nervous systems and other organic systems -- emerged.
This, if true, essentially means that no intelligent or coherent reason for the existence of life was considered beforehand by a rational entity in some planning stage. In the absence of any good reason to take the notion of a planning stage seriously, or the notion of there being a valid goal in preventing the decay of material systems, we more or less have to conclude that the brain's ability to create sensations in reaction to stimuli is not only unintelligent, but downright nasty.
So, to reiterate: Nothing capable of being received by a brain as sensory input is possessed of some innate unpleasantness; the brain, as part of the central nervous system, is chiefly interested in preventing the genotype, as an energy-dependent process, from decaying; to this end, the brain creates unpleasantness as a reaction to the "efforts" of external agents to bring about disorder in the system; however, there isn't any intelligent reason to believe that entropy is something to be stopped, making the brain's extremely painful efforts to stop it really unnecessary and unintelligent; additionally, every brain has failed or will probably fail in its efforts, and over 99% of them no longer exist.
Pain is bad, but is termination of life? If we were completely incapable of feeling anything, but, unlike bacteria, still possessed language, would we really mind dying? Are shark teeth bad because they can damage our organs, or because they hurt? Would you mind a lion ripping your guts out if it didn't hurt or cause intense fear, and if not, why is that a bad thing? If the AIDS virus were the only entity in the universe capable of replication, it would no longer be a terrible virus, for what do rocks care if they "get AIDS"? Brains create pain to preempt decay; get rid of all the brains and you could have a universe composed of nothing but AIDS -- with no problems whatsoever, anywhere.
The next time that you try to avoid a horribly painful situation, remember that it's not the world that you should be fearing -- it's your brain and its childish insistence on resisting entropy. Bullets, kidney stones, births, panic attacks? Bone cancer? They're okay in themselves. Really, the only thing that's actually capable of hurting you is that pink thing in your skull.