There are many facets to depression and to analyse them from a evolutionary perspective, you should evaluate every single one them separately.
Some specific types of depression, like 'burn-out' syndrome, have an obvious cause-effect and is some sort of defense mechanism to protect you from yourself. The metaphor of getting burned is fitting, because it's similar to physical pain.
But evolution doesn't bother if something is beneficial or not. A common mutation can be devastating for the individuals mental health, but as a species we can have evolved to circumvent this weakness by triggering certain behaviors to prevent us from reproducing. The courting behavior in animals is also often a test the behavior of a potential partner; unexpected behavior leads to rejection.
Genes do not evolve into cleaner, more efficient code, but rather into super complex buggy spaghetti-code that somehow works. We may have evolved to be somewhat intelligent, but we are also inherently flawed.
An interesting observation in that regard was the deranged penguin (documentary: Encounters at the End of the World).
Since there are intelligent people here who follow the topic of evolutionary psychology, I'd like to hear opinions about some research from 2009. Particularly if this idea seems reasonable or not, but possibly other opinions that people might have about it.
The idea is a variation on one that's somewhat popular here: that some conditions usually regarded as mental illnesses (Asperger's for example) are beneficial, even adaptive. But the condition in question now is depression. Briefly, the argument is that depression, at least when it is a response to stimuli and not a permanent feature, can have the useful effect of encouraging more rational thought when this is particularly important, even at the cost of quality of life, and that this is adaptive.
Links: a Scientific American article, a journal article (which I haven't read, behind a $12 paywall). Here's the abstract of the journal article:
The full journal citation is Andrews, Paul W., and Thomson Jr., J. Anderson; July 2009; Psychological Review 116 (3), 620–654; doi 10.1037/a0016242.