Of course, all evolution occurs by natural selection. Traits that help an animal survive are passed down. Those that do not help it are lost.
The ancestors of the whales were land animals that had hooves. We know that sometime around 52 million years ago the ancestors of whales were swimming in the oceans, but still had small hind legs that were useless for walking on land.
As to why whales are so big, the most likely answer is that the large size is needed for keeping them warm. Beings with more mass to surface area keep warm more easily. Whales need to be well insulated to live where they do. They also need to carry lots of fat reserves so they can fast for a long time during their migrations.
It is generally agreed that the evolution of whales (cetaceans) began 60 million years ago with Sinonyx (Mesonychids) a carnivorous undulate (hoofed animal) the size of a wolf. Key features of this fossil are its triangular shaped teeth and skull. The next step in the development of the whale was Pakicetus, 52 million years ago (Early Eocene), an animal with long tail and thick neck and hooves. Amulocetus, 50 million years ago, developed a body better adapted for swimming than walking on land. It is thought that ambulocetus filled a nitch similar to alligators and crocodiles.
Rodhecetus, 46 million years ago, had large limbs that could support the body on land, and swim in the oceans. It is likely that they were amphibious mammals. Basilosaurids and dorudontids, 35 million years ago, possessed the physical characteristics recognized as whales.
Squalodon, 33 to 15 million years ago, developed characteristics similar to odontocetes (toothed whales) and Cetotherium, 30 to 14 million years ago,developed characteristics similar to mysticetes (baleen whales).
Whales size provides them with biological advantages, protection from environmental factors such as predation, temperature and pressure. The open ocean provides the nutrients to maintain a large body mass.
Coenozoic era, upto the present, has had an estimated duration of about 63 million years. It could be called as "age of mammals" . The era has been divided into two periods- Tertiary and Quarternary. The tertiary period is characterised by the spectular rise of mammals. The tertiary period has been separated into four epochs, namely Eocene, Oligocene, Miocene and Piocene.
The term Eocene has been derived from two Greek words meaning "dawn" of "modern life". It marks the beginning of tertiary period.The most significant event was the diversification of placental mammals and by the end of Eocene many groups of modern mammals had come into existence. By the end of this epoch two groups of mammals, whales and sea cows returned to sea.
Based on morphology, scientists have long thought that the hippo's closest relative is the pig. This theory has seen a spectacular reversal twenty years ago, when tests have made first molecular analysis and then confirmed by DNA analysis. These genetic studies have shown that the hippo's closest relative is ... whale.
The problem now was that there were not any fossil evidence to support this discovery. How looks like the common ancestor of Hippos and whales? The answer to this question was discovered by Jean-Renaud Boisserie from UC Berkeley and French colleagues Michel Brunet and Fabrice Lihor: whale and hippo ancestor was a semi-aquatic mammal that lived 50-60 million years ago and has disappeared during the Ice Age.
This animal gave birth to two different groups: first cetaceans, which gradually became fully aquatic (of which resulted whale, orca, dolphin) and, on the other hand, a very diverse group of animals called Artiodactyla , which was mistakenly believed it would be related to pigs. Artiodactyla existed along the almost 40 million years and were extremely diverse, grouping them specialists in 37 genera. They have spread all over the world except South America and Oceania. However, most of them died from less than 2.5 million years, the only survivor being the hippo. Currently, it is itself in danger of extinction. Hippos have been developed in Africa 16 million years ago and their number has seen a real explosion around 8 million years ago.