Soon after a sensor scan of an 87 million-year-old comet commences, alien artifacts begin appearing around the Enterprise, as well as a mysterious compass-design symbol, and sets of alien-looking icons that are grouped together in the same compass formation. The crew investigates, but the ship's computer is unable to identify the icons. Riker and Geordi realize that some kind of alien information has been downloaded into the ship's main computers from within the comet during the sensor scan. They decide to melt the outer shell of the comet to find out what its core contains. Before they do, however, Data realizes that he somehow has the ability to interpret the unusual symbols.
Phasers blast at the comet's outer shell until its contents are revealed — a huge, nearly solid, metallic object covered with the strange icons. Data somehow believes intuitively that the object is an information archives Geordi runs a diagnostic on Data in hopes of finding an explanation for his connection to the archives He is shocked when a web of alien circuitry forms in Data's head, and a compass design appears on his face. Suddenly possessed with a personality entirely unlike himself, Data smiles at Geordi and says "Masaka is waking."
Meeting with Picard, Data identifies himself as "Ihat" and says that Masaka will bring pain and death to the ship, and they must leave before she finds them. Then Troi enters, and Data cowers in fear, thinking Troi is Masaka. He first assumes the personality of a victim willing to be sacrificed to Masaka, and soon afterward, the persona of a frightened boy. Geordi deduces that the archive is using Data to recreate people from its culture. Then, the ship trembles as the archive sends an energy pulse through a tractor beam. Data, as Ihat, says that Masaka has arrived, and Ten-Forward is transformed into a tropical jungle. A sun icon is prominently displayed, and Picard realizes this must be Masaka's symbol.
Geordi reports that the trees and foliage in Ten-Forward weren't beamed over — the ship is actually being transformed by the archives Picard reluctantly orders the archive destroyed, but Engineering undergoes a transformation before they can act. Apparently, the archive is using the ship to recreate its own culture, so Geordi looks for the archive's transformation program, while Picard speaks with Data's "Ihat" personality, who reveals that Masaka can be summoned by building her temple, but fearfully declares that Masaka has found him and disappears before showing Picard the necessary sign. Ihat is replaced by another personality, Masaka's elderly father, who gives Picard the sign before he, too, vanishes.
Geordi locates the transformation program and inputs the symbol for Masaka, and a temple appears, complete with a throne. Picard and Troi notice a stone column featuring Masaka's sun symbol, along with a horn symbol in the background. Seeing this pairing elsewhere in the temple, Picard deduces that the horn must be Korgano's symbol, and eventually realizes that the relationship between Masaka and Korgano is like that of the sun and the moon, with one always chasing the other. Data then arrives wearing the mask of Masaka, and refuses to listen to Picard. Using the transformation program to replicate Korgano's mask, Picard confronts Data/Masaka and convinces her to give up the chase for the night and to resume it in the morning, just as she has always done. Masaka goes to sleep, the ship returns to normal, and Data returns, now devoid of the entire civilization of personalities that were within him.