In an irreversible expansion like this, the aveage pressure of the gas within the cylinder is greater than the pressure acting at the internal piston face $P_{ext} $ (where the work is being performed). Of course, at the internal piston face, by Newton's 3rd law, the pressure exerted by the gas on the piston face is equal to the pressure exerted by the piston face on the gas.
The reason that, in an irreversible expansion, the average pressure of the gas (averaged over the volume) is greater than the value at the piston faces is that the gas pressure is non-uniform spatially within the cylinder. This is the result both of mass times acceleration effects within the gas itself (yes, the gas has mass) as well as viscous stresses present within the gas that are proportional, not to the gas volume, but to the rate at which the gas is deforming locally within the cylinder. So the mechanical behavior of a gas experiencing an irreversible expansion is very different from that of a gas experiencing a reversible (quasi-static) expansion.
Now let's reconsider the force balance on the piston. If $P_{gas}(t)$ represents the instantaneous pressure exerted by the gas on the piston at time t, then the force balance on the piston becomes:
$$M\frac{dv}{dt}=P_{gas}(t)A-Mg$$If we multiply both sides of this equation by v=dx/dt and integrate with respect to t (from time = 0 when the piston is released), we obtain: $$\int_0^{x(t)}{P_{gas}Adx}=W(t)=\frac{1}{2}Mv^2-\frac{Mg}{A}[V(t)-V(0)]$$where W(t) is the work done by the gas on the piston up to time t, v is the velocity of the piston at time t, and V(t) is the volume of the gas at time t.
Basically what will happen here at short times is that the piston will rise and gain velocity, but it will overshoot its equilibrium position (like a mass on a spring experiencing simple harmonic motion). It will then slow down, until it begins moving downward. But it will again overshoot the equilibrium position.
If the piston were frictionless and the gas were inviscid, this oscillation would continue forever. However, even if the piston were frictionless, the gas is not inviscid. So the viscosity of the gas will act as a damping factor, and the piston, rather than oscillating forever, will experience a damped oscillation until if finally comes to a stop at the equilibrium position (i.e., when v = 0). So the equation for the work will become: $$W(\infty)=\frac{Mg}{A}(V_{\infty}-V(0))$$
So, even for a piston with mass, the kinetic energy effect will die out and the work will be the same as just the change in potential energy of the piston.