Chemical nutrients and energy tend to flow in the same direction for most of an ecosystem. The big difference is that the chemical nutrients are ultimately recycled in the ecosystem while the energy is ultimately lost from the ecosystem to the universe at large.
Energy in any ecosystem ultimately comes from the Sun. Sunlight is converted into food by plants. The scientific term for this is autotroph. The energy from the Sun then becomes chemical energy in the form of food. Primary consumers then eat the plants for energy. The energy moves up the levels of the food chain as larger animals eat smaller ones. All the while, much of the food energy is lost as work done by the animals' bodies and as heat (body heat).
Chemical nutrients follow a similar path in the beginning. Plants take nutrients from the soil and chemically combine them with water and carbon dioxide from the air to make food. The chemical nutrients are then passed to animals and along the food chain as animals eat each other. All the while, chemicals from the food are passed back to the soil as animals excrete waste and also from their decomposing bodies when they die. Bacteria and fungi called decomposers break down the organic matter from animals for food, releasing the chemicals back into the soil or into the atmosphere. These chemicals are then ready to be used again by plants to create food and start the cycle all over again.