iCloud Backup backs up the messages, or the message sync key in the case of Messages in iCloud being enabled.
iCloud Backup is not e2e encrypted, which means Apple either gets the full iMessage and SMS history (in the case of Messages in iCloud being disabled), or gets the e2e encryption key to decrypt the synced Messages in iCloud (which they already have, due to running iCloud).