A 2007 study by Shamay-Tsoory and colleagues, published in the journal Brain, suggests that the ventromedial prefrontal cortex may play an important role in the experience of gloating and envy. The ability to process these emotions may depend upon the cognitive ability to put oneself in another's shoes, to recognize that others have thoughts and feelings which differ from one's own. These are central aspects of the "theory of mind" , referring to a set of cognitive capacities which may be weaker among, for example, individuals with autistic spectrum disorders.
In a way, then, our capacity for schadenfreude is the "flip side" of our brains' capacity for empathy.
And we might not always be the one observing the suffering....recognizing that we may play a role in other people's experience of schadenfreude, the cast of Avenue Q remind us that "The world needs people like you and me who've been knocked around by fate. 'Cause when people see us, they don't want to be us, and that makes them feel great!"