The Squire of Gothos (Episode)
From Trekipedia
A powerful entity known as Trelane torments the crew of the Enterprise.
In Space Quadrant 904, eight days away from Colony Beta VI, the Enterprise is trapped in orbit around an uncharted planet. There, Captain Kirk is confronted by Trelane, an illogical but extremely powerful alien. Although he appears to be an adult humanoid fascinated with the history of Earth, Kirk reasons there is more to him than his outward appearance—and challenges him to a duel. When Trelane loses, he sentences Kirk to hang. In the nick of time, the alien's parents appear to rescue Kirk and the Enterprise from their playful son.