When she says he lost his head, she is saying the truth in a teasing way. His head was cut off with a guillotine:
His head being shown to the crowd.
When E.Nesbit says that the king in her story "kept his head," she is making a joke -- a pun -- by using the same words to mean two different things, "kept his head" means that the king's head didn't get cut off and "kept his head" also means that he didn't get into a panic (the saying "keep your head" means that you keep calm -- you keep thinking). Jokes don't sound funny when you explain them, I know; but after awhile of reading these little explanations about how E.Nesbit uses words you'll be able figure the jokes out for yourself, I bet.
Go back to the story "The Charmed Life."