The energy comes form the breaking of bonds formed between the carbon and other atoms. These bonds when break they release energy. The energy in the bonds came from the matter such as bones from dead animals and other bodies containing high content of carbon which were subjected to high temperature and pressure situatoins under the ground which refined these forms of matter to the present form of the fossil fuels that we know.