The above scenario (i.e. where the writer sells the song, including all rights, to the artist) is very common, for the reasons already stated. To put one of the reasons that happens slightly differently, some of these writers simply do not have the leverage to insist on retaining rights to the songs. Think about it. There are LOTS of song writers out there. If you wrote a cookie-cutter potential pop hit, but want to retain certain publishing rights, and writer Y out there wrote one and is willing to sell away all rights, whose song is Taylor Swift (or Britney, or whoever) going to buy?
But some artists do write songs and retain the rights. Go back to the site I posted above. Actually, here it is:
https://www.allmusic.com/album/contagious-mw0000196388 Look at track #3. Al Pitrelli was not in the band, but he co-wrote that song. And Taylor Rhodes shows up on tracks 1, 8, and 9. He is (was) a professional writer for a lot of bands. He was not in this band. But the band and label wanted to use him to try to get some potential "hits." Or think of a guy like Prince or Bowie, who wrote tons of songs for tons of bands. Or another professional writer like Desmond Child, who is credited with You Not Me, for example, or who wrote/co-wrote the Scorpions album Humanity: Hour 1, and has done the same for tons of other artists. There are lots of guys like this. Sometimes they retain the publishing rights; sometimes not. It just depends on the contract. But there is money to be made either way.