There are references to melancholia as far back as Socrates, but I'd like to know when it actually became documented in history as an ailment to be cured. Did some cultures recognise it earlier than others?
You're thinking of Hippocrates, and that is when it was thought of as a disease. Hippocrates was a physician, and he identified melancholy as a mental disorder caused by imbalance of the humors, namely an excess of black bile. Traditional treatments involved a change in diet, combined with fresh air, exercise, and listening to music.
I've written a bit on this before so feel free to check out my previous answer, However only some aspects of that original answer actually answer your question so I'll post the relevant sections below and if you have any follow-ups feel free to ask:
As you know early asylums & psychiatry itself was fairly unscientific with Psychiatry dating back to the ancient greeks. Psychiatry in the Greeks mind was the field of the spiritualists & that of the supernatural with exorcisms being commonplace, this was the definition of Psychiatry until the mid 19th century when it started to be recognised as a medical discipline & not just a subsidiary of Philosophy.
However there were some people that thought those with mental illness should be treated in a humane way (even if their theories as to what was causing their distress & how best to cure it was distorted by the limitations of the age) This "humane treatment" does date back to the greeks & romans but I will say that I'm not too familiar with their methods so I can't go too in depth: In the 4th Century BC, No other than Hippocrates:
"theorized that physiological abnormalities were causing mental disorders. He viewed the brain as the seat of consciousness, emotion, intelligence, wisdom and reasoned that disorders involving these functions must be located in the brain and were not inflicted by the Gods."
And in 129 BC, Greek physician Asclepiades advocated humane treatment of the mentally ill, he freed them from confinement & gave a prescription of diets & massages. Roman physician, Galen;
"adopted a single symptom approach rather than broad diagnostic categories, for example studying separate states of sadness, excitement, confusion and memory loss. Galen extended these ideas and developed a powerful and influential school of thought within the biological tradition that extended well into the 18th century."
Skipping gleefully forward into 8th Century Baghdad, we see some of the first psychiatric hospitals. These hospitals were humane as muslims believed the mentally ill were incapable of doing things yet deserving of treatment. You can see here in this painting of a Muslim doctor treating a mentally ill patient. This treatment can be linked to the Islamic belief of "Greater Jihad" (أكبر الجهاد; al-jihad al-akbar), Whereas "Lesser Jihad" (الجهاد الأصغر; al-jihad al-asghar) is an outer physical struggle against the enemies of Islam, Greater Jihad is the struggle against oneself.
Now that we've given a little bit of tangential context let's get down to the diagnosis section of your question. Melancholy or Melancholia were terms that were consistently used to describe what we now call clinical depression. The earliest use of depression instead of melancholia was in 1856 by French Psychiatrist, Louis Delasiauve and there after it started appearing in medical texts.
By the end of the 19th Century, Melancholia & Depression were frequently used terms however the latter started to become more frequent thanks to Psychiatrists such as Emil Kraepelin (who coined the term depressive states) and Henry Maudsley (who founded the Maudsley hospital & coined affective disorder)
The first time depression became a "formal" medical diagnosis (even though there were bits and bobs in between) was in the 1st edition of the DSM (1952) where it was defined as both depressive reaction, an emotionally unstable reaction to an (un)identifiable event & depressive neurosis (coined in DSM-II), an emotionally unstable reaction to an internal conflict (similar to the Islamic concept of greater jihad)
From then on there were developments in our modern understanding of the symptoms & treatment of depression. Hopefully this helps :)
Sources (as well as the other sources mentioned in my original answer):
Berrios. E. G. (1988); "Melancholia and depression during the 19th century: a conceptual history", British Journal of Psychiatry
Davison. K (2006); "Historical aspects of mood disorders"
Lewis. J. A (1934); "Melancholia: A historical review", Journal of Mental Science
American Psychiatric Association (1952); 1968) "Diagnostic and Statistical Manual of Mental Disorders", 1st & 2nd Editions.