There are countless examples of fictional literary works that give the reader a history lesson through its content. Harper Lee's novel, To Kill a Mockingbird, immediately comes to mind. Although Lee bases some of the scenes and characters from her own childhood, the novel is largely fictional, and its messages of racism and life during the Depression-era Deep South are wholly accurate and believable. There are many moral lessons to be learned in the story, but its historical content (especially dealing with white/black bias, gender bias, and treatment of the mentally challenged) teaches the reader as well. Many of Charles Dickens' novels also offer historically accurate views of British life in the 19th century, as do most of Shakespeare's non-historical plays. In so many cases, the story line and characters may be fictional, but the story that is told is totally relevant to the historical period.
There is an entire sub-genre of fiction called "historical fiction" where the intent of the author is to create a picture of people of a particular time and place in history. These works can be based on people who actually lived, or on fictional people that could have lived in a particular time period. In terms of popular fiction, the novels of Phillipa Gregory are a good example of this kind of fiction. She writes about the life and times of the Kings and Queens of England, and her novels are based in historical fact, but she also takes creative liberty with the story details in order to craft the complete story. They are not meant to be a history lesson, but they can provide a snapshot of the times.
We have to be careful about doing this, but if we are careful, fiction can show us what people believed during a given time.
For example, the book Uncle Tom's Cabin can tell us something about the time in which it was written. We have to be careful, though. We can't assume that its depictions of slavery are accurate because it was written by an anti-slavery Northerner. We can't assume that all Northerners believed in Stowe's ideas either. But because the book sold so well in the North, we can assume that many people there shared this particular view of slavery. In this way, we can use fiction to shed light on what things were like during the period in which the fiction was written.