Pranks can do more than provide a little comic relief. They can teach important lessons. Like, "Don't take yourself too seriously." Or "Here's a taste of your own medicine." Or "I'm a dick." Here are pranks from science fiction and fantasy, some of which say something of value and some of the "I'm a dick" variety.
A lot of pranks rely on surprise of some sort. Some are parts of plot twists. Therefore, this whole article has a massive spoiler warning attached to it.
A lot done by Trelane and Q is some form of prank by bored, nearly-omnipotent beings. Q gives Riker Q powers and bets on it. Q puts humanity on trial. Q turns the Enterprise into Sherwood Forest. Q appears as God and moves Picard through time. Q introduces the Federation to Borg. Hahaha! His son was more annoying but less massively destructive: he removes clothes, has a computer talk back to the captain, removes Neelix's vocal chords, and apparently shifted a planet's tectonic plates.
The lesson: Powerful beings are always going to mess with you.
Look, we've said it before, and we'll say it again: There's something wrong with Starfleet Academy. At least this time the psychological torture isn't a required Starfleet activity, but the added stress of dealing with pranking classmates cannot be good for the head. In fact, we know it isn't, because it left enough of a scar on Kirk's psyche that, in "Shore Leave," his fantasy was to beat the man up. And when Wesley got to Starfleet Academy, Cadet Adam Martoni programmed Wes' sonic shower to spray him with mud. This earned Martoni a antimatter regulator spray of chili sauce.