The site Phrase Finder suggests:
Origin
The English author Francis Quarles wrote in Emblemes, 1635:
Wee spend our mid-day sweat, or mid-night oyle;
Wee tyre the night in thought; the day in toyle.
At that time there was a verb for working late by candlelight - elucubrate. Henry Cockeram defined that in his The English dictionarie, or an interpreter of hard English words, 1623:
"Elucubrate, to doe a thing by candlelight."
Clearly, we no longer have much call for that word and it has fallen out of use. Although it is probably some years since anyone needed to do it in reality the phrase 'burning the midnight oil' is still in everyday use.