When we heat any container containing a gas fitted with a piston, a part of the heat energy increases its internal energy and the rest does work on the piston.
But when the gas does work the piston, the piston acquires a velocity. How will the piston will return to rest after getting a velocity? If the outer atmosphere is present, the outer air pressure will oppose the motion of the piston and when the external pressure becomes equal to the internal pressure, the piston will have net force 0 acting on it. However, it'll still have a velocity, so it will move a bit further and then start oscillating. How will it come to rest? I have studied that the piston will come to rest in one of my books