Can I restore an iPhone backup taken from a previous iOS version, or is there a better way to retain my data?
Yes, this is possible.  The backups themselves contain application data and other information that isn't particularly version-specific (with the exception of going from 4.3 backwards, as pointed out by hobs above).

In fact, I had to do this exact process recently, going from 4.2.1 to an iOS 5.0 gold master on an iPhone using a backup, and it went off without a hitch.  Developers who want to install a prerelease OS version must put their devices in recovery mode, clean install the OS, then restore application data and settings from a backup.  I did this for my iPhone, which I hadn't upgraded to the latest 4.x OS, and everything came across cleanly to iOS 5.0.

The one thing to watch out for is that if you don't encrypt your backups (by selecting the "Encrypt iPhone backup" option in iTunes), your device's keychain will not be transferred to the new device. For non-encrypted backups, the keychain is only preserved when restoring onto the same device you backed up from.