The idiom most likely originates from the definition of ball as a large, social gathering where dancing is involved.
Ted: Did your dog enjoy himself in the dog park?
Vincent: Definitely. He had a ball! Literally and figuratively.
image courtesy of dreamstime.com