Work equation in thermodynamics
In a reversible process, the gas pressure is spatially uniform within the cylinder, and is described globally by the ideal gas law.  However, in an irreversible process, the force per unit area at the piston face is not equal the force per unit area at other locations within the cylinder.  Furthermore, the ideal gas law does not describe the behavior of the gas because viscous stresses contribute to the force per unit area for a rapid irreversible deformation.  So, even though Newton's 3rd law is satisfied at the piston face, unless we specify the force per unit area externally (e.g., manually), we will get the wrong answer if we try to calculate the pressure at the piston face using the ideal gas law.

In applying the equation $W=\int{P_{ext}dV}$ to calculate the work, $P_{ext}$ is supposed to be the force per unit area exerted by the surroundings on your system, at the interface between your system and the surroundings.  So, if the gas is your system, $P_{ext}$ is the force per unit area exerted by the inner face of the piston on your gas (and by your gas on the inner face of the piston).  In your example, if the cylinder is vertical and we do a force balance on the piston, we get:  $$m\frac{dv}{dt}=P_{ext}A-mg$$assuming there is vacuum on the outer face of the piston.  If we multiply this equation by the piston velocity v = dx/dt and integrate, we obtain:$$m\frac{v^2}{2}=\int{P_{ext}dV}-mg\Delta x$$Therefore, the work done by the gas on its surroundings (up until an arbitrary time) is given by:  $$W=\int{P_{ext}dV=mg\Delta x}+m\frac{v^2}{2}$$When the final thermodynamic equilibrium state of the system has been realized, the piston will no longer be moving (including any oscillations of the piston, which will have eventually been damped out by viscous stresses) and the work will then be determined by:$$W=\int{P_{ext}dV=\frac{mg}{A}\Delta V}$$
If, instead of vacuum, there is some constant force external to the piston (say, $P_{atm}$ equal to the atmospheric pressure), the previous results change instead to:$$W=\int{P_{ext}dV=mg\Delta x}+m\frac{v^2}{2}+P_{atm}\Delta V$$when the piston is still moving and $$W=\int{P_{ext}dV=(P_{atm}+\frac{mg}{A})\Delta V}$$at final equilibrium when the piston has been damped to rest.