Here is how a piston works in a standard four-stroke engine. During the intake stroke, the piston is pulled down by the crankshaft. While this is happening, the inlet valve opens and allows fuel and air to mix. For the compression stroke that follows, the inlet valve closes and the piston moves back up the cylinder to compress the mixture of fuel and air. During the power stroke, the spark plug ignites the fuel and air mixture, which pushes the piston back down. For the final phase, the exhaust stroke, the outlet valve opens. Powered by the crankshaft, the piston is pushed back up the cylinder. This forces exhaust gases out the outlet pipe.
Piston rings are essential to the functioning of a piston, as they provide a seal between the piston and the cylinder to facilitate better motion. These parts also prevent leaking between the compression and sump compartments.
A visual look at pistons working.