iOS4 has no jailbreak available, therefore you can't try and then restore if you have already updated.
If you have iPhone 3.1.2 or 3.1.3 then jailbreaks are available, and so far as I know, when you select restore in iTunes it restores your phone to the OS present in your last backup.
If I am wrong, could someone please confirm, but I don't think iTunes forces you upgrade to iOS4 if you do a restore.
I believe there already is a jb for iOS4. Haven't done it myself, but waiting to see it more mainstream before i jump in on my 3GS.
As for 3.1.2 it is jb-able with blackra1n
and 3.1.3 with spirit
You can restore your iPhone with iTunes after you make changes to the changelog on the iTunes you are using to restore. You will also have to have downloaded the OS version package you want to restore back to. When restoring, hold down the shift key while clicking on the restore button in iTunes and a browser window pops up, you locate your OS restore package (3.1.2 or 3.1.3) and start install.
If you simply choose restore, Apple forces you to latest OS. They don't sign certificates for previous OS versions. So unless you make changed to iTunes changelog, you won't be able to restore.
Lots of video on how to do this on the net. Google it.