This problem can be solved using the centre of mass. We can just calculate the change of the potential energy and that'd be the work done. That way, $W = MgL/2$ (since the cm is at $L/2$). I understood this method. But then, when I approached this problem with torque, I got a different answer. At the time of raising it from the end, the tangential component of gravity will be $mg\cos\theta$ (angle with the horizontal). So, the torque will be $mg\cos\theta\cdot L\cdot\sin90$. Integrating, we can find that the work done is $MgL$.
What went wrong with this procedure?