Squid ancestors were happy creatures with shells until about 400 million years ago, when the emergence of fish with jaws set off an evolutionary arms race between fish and cephalopods.
The shell was originally for buoyancy, but as the shell became a liability, some early squidlike creatures developed strategies to become more agile. One especially effective method involved internalizing the shell, which eventually shrunk down over millions of years to be a semi-rigid structure. Losing the shell meant squids could go deep in the ocean without breaking their shell, and as variations like octopodes developed from the common ancestors, they got even squishier and faster.
This is a really well-done explainer with lots of interesting information.