After the devastation of the Permian/Triassic extinction event many species were wiped out. Among those which made it through was Lystrosaurus , a strain of herbivorous therapsids which went forth into this depopulated world and multiplied and for a time most of the land vertibrates on the world were these turtle pig things. As their numbers swelled competition for food became intense and some populations were forced to the coastlines. While others grazed on ferns, these had to make do with what seaweed washed up onto the shore. A few came to swim out into the shallows to supplement, did well and those genes spread and were compounded on. As predators of various sorts arose to prey on the vast herds, they took to retreating to the ocean to escape them and a few million years into the Triassic you had critters like this guy.