How to restore OS X to out of box / factory experience prior to reselling? I'm ready to resell my Mac, and I'd like to prepare it for the new user, giving them factory-like out of box experience. Even better, I'd like the system to be fully updated, yet come up with no user accounts, starting into the initial setup as if one just unboxed it. Of course performing the updates and software preinstallation requires to set up a temporary user account :(
The instructions below were tested on OS X 10.6 and 10.7.

Preflight


Back up any data you wish to retain.
Make backup copies of iLife applications (iPhoto, GarageBand, iMovie, iWeb, iDVD).
Deactivate relevant licensed software such Adobe CS.
Unregister the machine from your iTunes account.


OS Cleanup


Boot from an external OS X install media, if you wish to completely wipe the hard drive. Boot from the recovery partition (hold ⌘-R on power-up) if you wish to retain the recovery partition as-is.
If booting from external media, use the Disk Utility to repartition the hard drive with one partition. Note: With SSDs, you ideally should first use a utility that lets you do a secure erase of the medium instead.
Use the Disk Utility to erase (zero out) the hard drive. This will take an hour or longer on a mechanical drive, and at least several minutes on an SSD. If it takes just a minutes, you didn't zero the disk!
Reinstall the OS X. Create a user account called factory.
Reinstall iLife either from media, or by copying it from the backup you've made in preflight.
Perform all system updates.


Restoring the Out-Of-Box Experience

Boot into the single-user mode by holding down ⌘-S during boot. A bare console will appear in a dozen seconds or so.


Remount the root partition in read-write mode.

/sbin/mount -uw /
Start the directory services.


OS X Lion and higher: 


launchctl load /System/Library/LaunchDaemons/com.apple.opendirectoryd.plist

Ignore the error related to com.apple.DirectoryServicesLocal.plist


OS X Snow Leopard: 


launchctl load 
/System/Library/LaunchDaemons/com.apple.DirectoryServices.plist
Access the directory services command line utility and remove the factory user:

dscl . (that's dscl, space, period)

list /Users shows the user list. The factory user should be there. That's the only one we care about. The usernames starting with an underline, those are system accounts that have to be left alone. Same goes for the  daemon, Guest, messagebus, nobody and root accounts.

read /Users/factory GeneratedUID shows the UID of the user, we'll need it below.

delete /Groups/admin GroupMembers &lt;uid&gt;, where UID is given above. Don't worry if you enter it wrong, you'll receive an error message. Press ↑ to go back and edit the command.

delete /Groups/admin GroupMembership factory

delete /Users/factory

exit
Remove the factory's home folder.

rm -R /Users/factory/
Remove the caches.

rm -R /Library/Caches/*
rm -R /var/vm/swapfile*
Remove the flag indicating that the initial setup has been done.

rm /var/db/.AppleSetupDone
Reboot or shutdown.

reboot or shutdown -h now