The answer would partly depend on what country you’re referring to, but for now I’ll focus on music copyright in the US.
Prior to the first Us Copyright Act, the American colonies each had their own forms of copyright laws. However, it was the 1790 Copyright Act that unified the copyright laws of music (and other works) throughout the nation. Under this act, the composer could own his/her song for a total of 28 years (though the copyright had to be renewed after the first 14 years) before the work entered the public domain. (This is a huge contrast to modern copyright laws; today the songwriter or copyright owner owns the song for his/her entire life plus 70 years.) Similar to modern copyright, this Act prevented sheet music from being reprinted without the composer’s consent. But in order for a song to be protected by copyright, the composer had to register the work with the United States government.
This remained the same until 1909 when Teddy Roosevelt set the second US copyright act into motion. Under this act, the duration of the copyright was doubled. Now the composer could own the song for 56 years (but again, the copyright had to be renewed after the first 28 years). Under this Act, a composer’s song was protected by copyright law the moment that it was published - no registration needed. To put this all into context, the phonograph was invented in 1877, but recorded music didn’t become widespread until the start of the 20th century.
It’s also interesting to note that, much like the laws of other countries at the time, the United States copyright laws didn’t protect the songs of foreign composers prior to 1886.
Just for clarification, I should also mention that there are two different types of musical copyright: the copyright of the song itself (the melody and lyrics) and the copyright of the sound recording (which didn’t come around until 1972).
I hope this helps!