I have been through several iPhones so far due to hardware defects (which I've outlined in other forum posts, for those who are curious). I have had 3 3GS phones in my possession, and every single one of them had the rattling you describe (and I didn't return the phones for that). I just chose not to be annoyed by a minor problem like that which doesn't affect the functionality of the phone.
However, what is interesting to me now is the fact that I also had 3 3G phones in my possession, too, and none of them had the rattling noise. So, it would seem that the rattling bit is very common to 3GS phones, but not to 3G phones (kind of like how the backlight leak/poor seal on the left side of the phone seems to be a very common 3G problem -- I had it on all three of my 3G phones, but again, like the rattle, it didn't bother me too much -- but NONE of my 3GS phones had that issue).
I have noticed that the rattling that occurs when you shake the phone only seems to happen (or at least is much more noticeable) when the mute switch is in the "silent" position. If it is in the normal position, it is either not present or not generally noticeable.
Since I took delivery on my last 3GS phone, which had the rattle but was not functionally defective in any other way, my mute switch on the 3GS has recently developed a defect (which I've also referred to in other posts here) where when it is in the silent position, it is very "touchy." You so much as breathe on it, and it will go ballistic, switching from silent to non-silent several times. It's like something is loose inside. The switch is so unreliable that, for now, I'm using an SBSettings toggle to turn silent mode on and off in software...I'm sick of returning phones to Apple for this kind of quality control crap.
I have also seen others complain about this same defect (touchy/loose mute switch) on their 3GS phones, too, and wonder if the loose rattle noise is a precursor to the problem on phones that will eventually develop the defective mute switch. I am 100% sure that the noise is coming from the switch (since it is much louder depending on the state of the switch) and not from the auto-focus camera mechanism, as other people have tried to claim.
The good news is that as long as you are under warranty, if you ever develop a defect with your mute switch, Apple is very good about customer service and I'm sure that they will replace your phone without a fight; it should be extremely easy to demonstrate/reproduce the problem in front of a Genius. I will probably get around to switching mine out eventually...
-- Nathan