Is it a modern 20th century thing with the advent of rock music, or is there a historical precedent for it in classical music?
There is significant precedent in classical music for the encore, though today I think it's safe to say that encores are much more common to soloist concerts than, say, orchestra concerts. (A story from my own experience: I once saw the Moscow State Symphony Orchestra when they were touring and they played a whopping FIVE encore pieces, which is sort of teetering on absurd for a classical concert. Lots of fun, though!) In the eighteenth and nineteenth centuries, the public concert (which I think is more relevant here) looked quite a bit different than what we have to today, where the code of conduct was looser (more of a social event than a sit and be quiet event) and the programming was more eclectic--concert programs would be made up of various genres, from a string quartet to opera aria to symphonic movement. If the audience really liked what they heard, they would demand the piece be played again. Unlike today, clapping between movements was common, so if a particular movement was very good (the music itself or the performance), then the audience would ask for it again. Same would go for operas--if an aria was well sung, the audience would ask for it again. So in a way, it was more of a genuine encore situation than today, where singers are more or less obligated to come back after they finish the set and do a cover or or something. Then it was really encore=do it again!
You can read about this in biographies of a number of composers from those centuries, such as Jan Swafford's Beethoven or Maynard Solomon's Mozart biography.