I think it comes from the Bible. Remember that story when the Jews only had a little bit of oil left to burn during their stay in the dessert, but the oil lasted the whole night
It is definitely of English origin. Henry Cockeram (1623) had interpreted it in "The English Dictionarie, an Interpreter of Hard English words" meaning to work late at night by candlelight.