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.