Not sure exactly when it dates from, but it was certainly well and truly before Lourdes.
In any event, the idea is that the candle is a visible symbol of your prayer. In the same way as incense is a visible symbol of prayer, since its smoke ascends to heaven as prayers do - ‘Let my prayer rise before you like incense’ as one of the Psalms says.
So does the flame (and sometimes the smoke) of a candle. And no, candles aren’t mentioned in scripture (they didn’t exist then) but lights are - ‘you are the light of the world’ ‘let your light shine before men’ and so on. And flame - remember the tongues of flame appearing over the heads of the Apostles at Pentecost? So candles are in a sense symbols of ourselves, the people who pray, as well as the prayers themslves. And of the Holy Spirit who intercedes for us when we cannot pray for ourselves, as St Paul says.
The candle expresses our hope that for as long as it keeps burning, the angels and the Holy Spirit keep your prayer before the Lord, even if you yourself cannot keep praying for the whole length of time that it burns. That’s one of the jobs of the angels - to present our prayers to God, a la Revelation.