As the Federation starship Enterprise, under the command of Captain Jean-Luc Picard, waits to rendezvous with the USS Victory, Chief Engineer La Forge and Commander Data go to the Holodeck to recreate a Sherlock Holmes mystery. Data, who has read all of the Holmes stories, recognizes and solves the mystery within minutes. Frustrated, Geordi storms out of the holodeck, leaving Data confused. In Ten Forward, Geordi explains that the fun is in solving the unknown; Data does not understand. Overhearing their conversation, Chief Medical Officer Dr. Pulaski asserts that Data is incapable of solving a mystery to which he does not already know the outcome. Data accepts Dr. Pulaski's challenge and invites her to join them on the Holodeck. There, Geordi instructs the computer to create a unique Sherlock Holmes mystery with an adversary who is capable of defeating Data.
In the new program, Dr. Pulaski is kidnapped, and Data investigates. They soon discover that Professor Moriarty is responsible, but when they find him with Pulaski in his hideout, they are shocked when they learn that Moriarty is aware of the Holodeck program being a simulation, and is able to access the Holodeck computer, showing them a sketch of the Enterprise he has drawn based on the computer's description. Data and Geordi leave the Holodeck to alert the Captain, and Geordi realizes that when he asked the computer to create the program he had asked for an adversary who could defeat Data, not Sherlock Holmes; as a result, the computer gave the Holodeck character Professor Moriarty the knowledge and sentience needed to challenge Data. When Moriarty gains access to the ship's stabilizer controls, Data returns to the Holodeck with Captain Picard.
Picard meets Moriarty, who demonstrates that he has evolved beyond his original programming and asks to continue to exist in the real world. Picard tells Moriarty that this would not be possible; instead, he saves the program and tells Moriarty that if they ever discover a way to convert Holodeck matter into a permanent form they will bring him back. Picard discontinues the program and the USS Victory arrives.