The piston starts at the top of the cylinder and moves down, increasing gas volume above it. This sucks a fuel-air mixture through the inlet valve. Pressure remains constant.
Inlet valve is closed, and the piston moves back up the cylinder, doing work on the gas and increasing pressure. Right before the end of the stroke, the spark plug creates a spark and ignites the mixture. Temperature + pressure suddenly increase at an almost constant volume.
The hot expanding mixture does work on the piston and moves it downwards. Work done by the gas is more than the work required to compress it (so net work output). Just before the end of the stroke, the exhaust valve opens and pressure reduces.