Yeah it's legal. The way that initiative rounds work sometimes leave little problems like this. Personally I choose to imagine it as the wizard backpedals away from the barbarian just long enough to cast his spell before the barbarian gets closer again. Of course since the wizard only moved a...