You have a collision between a moving mass $M$ and a stationary mass $m$ with $M>m$.
You have put a constraint on the collision viz both masses move off together.
Now suppose that constraint was not there and the collision was elastic what would have happened?
Momentum would have been transferred from mass $M$ to mass $m$ and mass $m$ would be moving faster than mass $M$ which would mean that mass $m$ was not attached to mass $M$.
Is it possible to have an elastic collision and at the same time mass $m$ moves with mass $M$?
One could drill a horizontal hole in mass $M$ and push mass $m$ into that horizontal hole at the appropriate time.
Mass $m$ would now suffer repeated elastic collisions with the top and bottom of the hole whilst moving with mass $M$ ie it would oscillate inside the hole.
In this way no kinetic energy (mechanical energy) has been lost.
In practice the masses might suffer permanent deformation which would require work to be done to break bonds; there will be an increase the temperature of the masses (heat has been generated) and sound may be produced all contributing to the loss of kinetic energy (mechanical energy) of the system.
Without the horizontal hole and if the collision is inelastic there needs to be a mechanism to keep the two masses together and whatever that mechanism is there will be a loss of mechanical energy from the system.