Memories may be encoded in their DNA or stored outside of their brain, Smithsonian Magazine writes:
The researchers trained flatworms to travel across a rough surface to access food, then removed their heads. Two weeks later, after the heads grew back, the worms somehow regained their tendency to navigate across the terrain, as recently documented in the Journal of Experimental Biology.
Interest in flatworm memories dates to the 1950s, when a series of strange experiments by Michigan biologist James McConnell indicated that worms could gain the ability to navigate a maze by being fed the ground-up remains of other flatworms that had been trained to run through the same maze. McConnell speculated that a type of genetic material called “memory RNA” was responsible for this phenomenon, and could be transferred between the organisms.
Subsequent research into flatworm memory RNA exploited the fact that the worms could easily regenerate heads after decapitation.
Previous research confirmed that the worms’ behavior is controlled by their brains, but it’s possible that some of their memories may have been stored in their bodies, or that the training given to their initial heads somehow modified other parts of their nervous systems, which then altered how their new brains grew.
There’s also another sort of explanation. The researchers speculate that epigenetics—changes to an organism’s DNA structure that alter the expression of genes—could play a role, perhaps encoding the memory permanently in the worms’s DNA.