I personally think your son may have been exposed to something he should not have been at such a young age. I think the proper thing, would have been to sit him down and ask why he was doing that, and explain that no one is suppose to touch anyones private areas. The other thing, is to pay attention to what books or television shows he may be exposed to. He is a child himself. Dont lose sight of that fact.
He obviously knows its wrong, thats why he did it in a closet and not in the open. You need to tell the mother of the 2 year old as well.
Well, now you've associated sexual curiosity with whipping. That's not good.
How old is he and how did he come to be unsupervised in a closet with a 2 year old where was the mom of the 2 year old ?.It could be that he was molested would there be any reason you think it possible ? Whipping/hitting will not help in fact will make matters worse .,I suggest you ask his Dad to speak to him and that you make sure he is not left alone with any child ,without supervision.