You say the machine will move when in 4wd but not in 2wd, right.
I'm thinking the engine drives the pump, the pump drives the hydraulic motor, the output from the hydraulic pump drives the transmission.
The transmission has 4 gear sets for 1,2,3,R, then the output for it goes to the rear wheels. Engaging 4wd also sends power to the front end.
Seems like the shaft going to the rear drive could be broken. Or the differential is broken as it will direct power to the left or right wheel that has the least resistance so if one side was busted somehow, no power to the other side.
So in summing up, power from the transmission powers the front and back differentials. I suspect a break in the rear differential othewise if the pump was bad both 2wd and 4wd would not work.