Optical protection of alkali-metal atoms from spin relaxation