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 and recreate a Sherlock Holmes mystery. Data, who has read all of the Holmes mysteries, is able to solve the mystery within minutes. Frustrated, Geordi terminates the holodeck program leaving the holodeck with a confused Data close behind him. In Ten Forward, Geordi expresses his frustration in experiencing a Holmes mystery to which Data already knows the outcome, and explains the fun is in the mystery itself. Data explains he doesn't understand what Geordi means. Overhearing their conversation, Chief Medical Officer Dr. Pulaski tells Geordi Data is incapable of reasoning a Sherlock Holmes mystery outside of the volumes of the adventures already written. She also tells Geordi Data is incapable of independent deductive reasoning at all. Data accepts Dr. Pulaski's challenge and invites her back to the holodeck with Geordi and him. At the holodeck, Geordi instructs the computer to create a new program with a unique Sherlock Holmes-type mystery and an adversary who is capable of defeating Data. While in the program Dr. Pulaski is kidnapped. Data proceeds to solve the mystery with deduction. They soon discover that Professor Moriarty is responsible, but are shocked when they learn that Moriarty is aware of the Holodeck being a simulation. Moriarty is able to access the Holodeck computer and shows them a sketch of the Enterprise he has drawn.
Data and Geordi leave the Holodeck to alert the Captain. 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 briefly gains access to the ship stabilizer control, Data and Geordi return to the holodeck with Captain Picard.
When Moriarty and Pulaski are found, Picard is able to negotiate the return of control of the ship's computer. Moriarty, who now knows about the existence outside the Holodeck, 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.