When you delete a file or format a partition, the data is not actually erased. It's still there in the same place. Formatting and deleting simply removes the record (index) of where the data is. Therefore, the OS thinks there is nothing there. This is why the data can be recovered.
However, if you save other data after deleting/formating, the OS thinks the space is empty and may overwrite previous data. In that case, the older data is gone for good.