How does factory reset work?
Generally speaking the factory reset function you mention will restore any saved variable data information back to default values. It is not true that all embedded devices have this capability. Some do but not all.

If you want your device to support a return to factory default firmware itself then your design has to incorporate a memory into the circuit to store that image. A common component for this is a SPI flash chip. Then your boot loader also needs to be changed to support a mode of getting a firmware image from the SPI flash chip instead of getting the image in through the serial port. If your device does not support user replaceable firmware then it is usually not necessary to provide a means to restore to factory default firmware.