In most cases, this isn`t a problem. It`s perfectly natural for birds to masturbate from time to time. There are are few ways this does become a problem, though. Those are:
YOU are the object chosen to masturbate on.
it becomes excessive/obsessive.
it triggers territorial/aggressive behaviors.
If your bird tries to masturbate on you, put him down and walk away for a few minutes. He should catch on that this is not encouraged.
If it becomes excessive and/or an aggression problem, rearrange the cage, limit the hours of light, and remove any specific object that he`s chosen to be the recipient of his attention.
If it`s just something you catch him doing once every other day or so... just ignore it. Draw no attention to the behavior that would encourage it. Just let him do his thing and pretend it never happened.
I`ve had birds masturbate in the past... but I`ve never had a masturbating PROBLEM in the past. So I always simply ignored it.