My son did that once, when he was five. It turns out that when I had the kids go into quiet time in their rooms (30 minutes, all by yourself-time), my son thought he simply wasn't allowed to come out of his room for any reason, even to go to the bathroom, so he peed in his closet. :(
I clarified that rule for him - and reinforced all of the stuff he needed to know (bathroom is the only place we pee in the house, etc.; we don't disrespect our home by doing this, etc.), and he hasn't done it since.
It doesn't sound like my experience is your issue, but if you rule out abuse/trauma, I'm going along with "Weird Things Boys Sometimes Do." When my ds turned five, he was fascinated that I told him it was okay to pee behind a tree in our backyard if he couldn't make it inside. All of a sudden, he was just waiting to pee until he could get outside to do it - there was something inherently awesome about peeing outside, and he experimented with different trees and everything (maple vs. birch vs. pine...). /shrug/ Kids are weird sometimes. ;)
I vividly remember my mom getting me ready for a bath, standing next to the tub, and she had forgotten a towel or something. She left the room and said "don't move, I'll be right back." I had to pee and so I just did on the floor. She got upset and I remember thinking, "but you told me not to move!"
My guess would be he did it once out of curiosity, didn't get caught, and then it became a game. He was doing something "wrong" and so he got the feeling he was "sneaking" and probably liked it because sometimes it is fun to do "wrong." I wouldn't do anything beyond explaining why he can't do that anymore. If it continues, then I would come up with a punishment.
I have Stiff Person Syndrome and my other car is a candy apple red Rascal. Feel free to ask me about it.