Ed and John have the right answer. For completeness, here is the solution
using inertial frames exclusively (Ed really used the accelerated frame of
the moving pulley) :
For the 2 kg mass to stay put requires T2=2g
Forces on the massless pulley add to zero, requiring T1=2T2 = 4g
Apply F=ma to M: 4g-Mg = Ma1
Apply F=ma to the 3kg: 3g - 2g = 3a3
For the 2 kg to stay put: impose a3=2a1, as John argued.
{My previous attempt omitted this 2!)