When you delete or clear out messages from the iPhone, it doesn't really get deleted completely (unless, of course, you do a full restore without using a backup). And thanks to this feature, you can retrieve text messages stored in backups.
If you connect your iPhone to iTunes and sync it routinely, there's a strong chance that you are also backing up data in the background. This data includes, amongst other things, text messages. When you restore from a recent backup, you'll not only get the contacts, photos, videos and apps back, but you'll also notice that messages are back.
Using a Third-party Recovery Software
Apps like iExplorer, iBackupbot and CopyTrans help you recover data from iTunes backups that exist on your PC/Mac. The backup file is usually compressed in a way that makes it impossible to read them like normal files. The SMS file/iMessage, for instance, is stored as an sqlite file and you wouldn't even be able to open it correctly.
Third-party backup files like the ones mentioned above help you decompress the backup file and show you what these contain. Photos, videos, contacts, emails, calendars and text messages are all part of the backup. You want just the text messages? Just run the program and it will find the backup file and show you the content.
User interface and methods vary depending on the software being used. But it's mostly easy to find the control for text messages. Most backup explorers for iPhone will help you locate individual text messages and extract them out.
If you take some trouble to locate a particular file within the backup location, there's a free service at iPhone-SMS.com which will help you get a readable copy of all your SMSes/iMessage in Excel, PDF or HTML format. The trick is you should have backed up your iPhone via iTunes on PC/Mac and you should not have encrypted the backup. (this you can do by deselecting Encrypt local backup in the Backup options).
The website has the instructions and it works for iOS 7 backups too.
Incidentally, these are the only two easy ways to extract and retrieve text messages or iMessages when you don't have an iPhone or and iPad to restore the backup to. But wait. What if you don't have a backup at all? In that case, retrieving text messages becomes impossible.