Given the changes that have occurred, his behavior might be an attempt to ingratiate himself with peers at school. It is a good sign that his problematic behavior does not occur across the board or on a consistent basis. This would indicate he probably does not display a serious mental or emotional disorder. However, talk things over with school personnel and see if they think he could benefit from an evaluation by a pediatric mental health professional. The challenge is to distinguish between some fairly benign, normal-spectrum misbehavior (which, of course needs to be managed by some sensible limit-setting and appropriate consequences) and something of a more serious sort.
maybe he is trying to look cool for friends or trying to make friends. and i would like to thank you for the comment you put at the question "masturbation" i could not believe that kid! well good luck!