They didn’t! They essentially “held their breath” on land for short periods before going back into the water. Essentially the reverse of what a lot of land animals do – including humans! Over time they evolved to hold their breath longer and longer, which eventually evolved into a way to breathe air.
Latest Answers