An ecosystem is an open system: both energy and matter enter and leave it. For most ecosystems the energy comes from sunlight. Producers fix it into carbon compounds, and it then passes along food chains when one organism eats another.
Energy flows one way and is not recycled. At each step most of it is lost, mainly as heat from respiration, so only about 10% reaches the next trophic level. This is why food chains are short.
Matter is different: it is recycled. In the carbon cycle, photosynthesis takes carbon dioxide out of the air, and respiration and combustion put it back. Decomposers release the carbon held in dead matter.