Erase All Contents And Settings on iPhone 4
The tool you want to use is iTune's Restore to Factory Settings, found in the General tab when your iPhone is connected to iTunes.

While I haven't used this on an iPhone, I am reasonably sure that it will remove your iOS (firmware) updates so that you will have to re-download them.
It will also take all your apps, music, videos, and other media off the iPhone, as well as erasing all your settings and resetting them to the defaults. Basically, your iPhone will be just like it was when you bought it.

Directions on how to perform this operation (and restore afterwards from a backup) are available in the Restoring section of this Apple help document.