Solar energy is the ultimate source of the energy for fossil fuels.
Plants absorb sun's energy through photosynthesis when they are alive and the animals that eat them, get the energy from plants. This energy stays trapped in them for millions of years and gets converted into fuel. Thus, the energy released when fuel is burnt, originally comes from the sun.