It is the action of the monster moving out of reach, while the monster is focusing on a different action (in this case, the action of attacking a new target), which provokes the attack of opportunity. If the monster is focusing on not provoking (ie, the Withdraw special movement action), then...