Why is direction of friction in the direction of motion and here it is the platform that moves....the movement is not caused by friction so how is friction doing work?
In fact, the motion is caused by the friction. Imagine there was no friction. Then the platform would just slide away underneath the box, and the box wouldn't follow along. The box wouldn't start moving along, because there would be no force pulling it along.
But the box does move along with the platform. And which force is pulling it along? That would be static friction.
In the second one there is no slipping either so let the lower block be the platform like the previous case. Why is friction in the opposite direction for the same case?
In fact, the friction does point along with the motion, towards the right.
- If you look at the upper block $m$, then the friction does pull it along with the lower block, just as explained above.
- If you look at the lower block $M$, then the friction pulls opposite, so towards the left. That is because the upper block $m$ is an extra burden for the lower block to pull along - it is a bit "tougher" to make the lower block $M$ move. It is this friction towards the left that makes it "tougher" to move the lower block.
So, be careful with which object you are looking at when considering forces. On one object, the force is one way, on another object, the force is opposite. This is Newton's 3rd law, and these two equal-but-opposite friction forces are the action/reaction force pair in Newton's 3rd law.