An early version is 'as happy as a clam at high water'. Clams are free from the attentions of predators at high tide, so perhaps that's a reason to consider them happy then. The earliest known citation doesn't mention water though. That's in Harvardiana, 1834:
"That peculiar degree of satisfaction, usually denoted by the phrase 'as happy as a clam'."
John G. Saxe, the American writer best known for his poem The Blind Men and the Elephant, used the phrase in his Sonnet to a Clam, in the late 1840s:
Inglorious friend! most confident I am Thy life is one of very little ease; Albeit men mock thee with their similes, And prate of being "happy as a clam!" What though thy shell protects thy fragile head From the sharp bailiffs of the briny sea? Thy valves are, sure, no safety-valves to thee, While rakes are free to desecrate thy bed, And bear thee off, - as foemen take their spoil, Far from thy friends and family to roam; Forced, like a Hessian, from thy native home, To meet destruction in a foreign broil! Though thou art tender, yet thy humble bard Declares, O clam! thy case is shocking hard!
The phrase originated in the US and possibly before 1834. In 1848 the Southern Literary Messenger - Richmond, Virginia expressed the opinion that the phrase "is familiar to everyone".