Depending on who you ask, science fiction goes as far back as antiquity, or as recently as the twentieth century. This has a lot to do with how you define science fiction, and it's a subject of fierce debate among literary historians who focus on this genre. Given this debate, I think it's helpful to establish a definition. James Gunn, one of the leading scholars on SF and an author himself, defines science fiction in its simplest terms as "the literature of change." Refining his definition, he states that:
Before science fiction could be written people had to learn to think in unaccustomed ways:
- they had to learn to think of themselves not as a tribe, or as a people, or even a nation, but as a species;
- they had to adopt an open mind about the nature of the universe - its beginning and its end - and the fate of man
- they had to discover the future, a future that could be different from the past or the present because of scientific advance and technological innovation.
So you can see here that Gunn believes science fiction is a fairly modern invention (Gunn, The Road to Science Fiction, x).
Most scholars agree that you can find the earliest examples of science fiction as we know it in the 19th century, though we didn't call it science fiction until the 1920s. Many of SF's seminal texts emerged from the gothic tradition. For instance, Mary Shelley's Frankenstein (1817) became an extraordinarily influential work that has remained part of science fiction's lineage ever since - although her legacy is sometimes second guessed by SF scholars, including noted SF historian Brian Aldiss. Aldiss considers Jules Verne and H.G. Wells to be far more important in the development of the genre, as they established a number of important tropes. Verne's work was packed with the application of real science to fantastical situations, and this became an important trait in separating science fiction as being a more serious genre than, say, romance. Wells's science was considered not to be quite as solid, but he was instrumental in establishing science fiction themes that would be repeatedly adapted in the decades to come - including alien invaders, secret serums, and human experimentation. There are many other notable examples that could be classified as sci-fi from the 19th century, including Edward Bulwer-Lytton's Vril, The Power of the Coming Race, Robert Louis Stevenson's Doctor Jekyll and Mr. Hyde, and some of the imperialist fiction of H. Rider Haggard.
The genre crystallized in 1926, when Hugo Gernsback coined the term "Scientifiction" - later simplified to "Science Fiction" - in his pulp magazine, Amazing Stories. Prior to Gernsback establishment of Amazing Stories, science fiction was usually classified as romance, and thus had no genre boundaries or conventions. However, the pulps were heavily editorialized, and men like Gernsback and, later, John Campbell, went to great lengths to establish exactly what a science fiction story was. Many of the most famous science fiction authors of the early 20th century got their start first as fans, and then writers, for the pulps - including Isaac Asimov, Arthur C. Clarke, and Robert Heinlein.
So the short answer to your question is - no, people in earlier epochs did not write science fiction (although that depends on how you periodize our current epoch). I'm with James Gunn in the opinion that SF is a modern invention.
Sources:
Aldiss, Brian. The Trillion Year Spree. London: Victor Gollancz, 1986.
Amis, Kingsley. New Maps of Hell. London: Victor Gollancz, 1961.
Carter, Paul. The Creation of Tomorrow. New York: Columbia University Press, 1977.
Gunn, James. The Road From Science Fiction: From Wells to Heinlein. New York: Scarecrow Press, 2002.