What you are referring to is something called dissociative fugue. It is characterized as an official psychiatric disorder and dissociative disorder in the DSM-5, and its prevalence has been estimated at 0.2%, though it is much more common in connection with wars, accidents, and natural disasters.
The disorder is characterized by reversible amnesia for all aspects of the self: identity, personality, memories, personal history, etc. These episodes of 'fugue' can be caused by stress (in those with the characterized disorder), but similar episodes are not considered to be 'fugue' if they were induced by other factors, such as drug ingestion, head trauma, or other conditions such as dementia. The fugue itself typically lasts anywhere between a few hours and a few days, but can occasionally last a lot longer (up to several months). It usually involves unplanned travel or wandering, and in some cases can lead someone to form a new identity.
After recovery, the person typically has no recollection of what happened during their amnesiac state. However, they typically remember everything that they had previously forgotten about their identity. If the episode was caused by a specific stressor (e.g. an abusive figure), the stressor may remain forgotten upon recovery.
For the cases in which the amnesia was caused by a traumatic event, drug use, or physical trauma, then it is usually referred to as retrograde amnesia. Similar to dissociative fugue, those who undergo retroactive amnesia typically make a spontaneous recovery.
Retrograde amnesia usually follows damage to areas of the brain other than the hippocampus (the part of the brain involved in encoding new memories), because already exisiting long-term memories are stored in the neurons and synapses of various different brain regions...It usually results from damage to the brain regions most closely associated with declarative (and particularly episodic) memory, such as the temporal lobe and prefrontal cortex. The damage may result from a cranial trauma (a blow to the head), a cerebrovascular accident or stroke (a burst artery in the brain), a tumour (if it presses against part of the brain), hypoxia (lack of oxygen in the brain), certain kinds of encephalitis, chronic alcoholism, etc.
Retrograde amnesia is often temporally graded, meaning that remote memories are more easily accessible than events occurring just prior to the trauma (sometimes known as Ribot's Law after the 19th Century psychologist Théodule-Armand Ribot), and the events nearest in time to the event that caused the memory loss may never be recovered. This is because the neural pathways of newer memories are not as strong as older ones that have been strengthened by years of retrieval and re-consolidation. While there is no actual cure for retrograde amnesia, “jogging” the victim’s memory by exposing them to significant articles from their past will often speed the rate of recall.
(Source)