The question is – Why was Ganesha’s head severed and replaced with an elephant head?

Was it just an accident that Shiva beheaded Ganesha in the battle, not knowing who he was?

Not really.

If you look closely at the stories from Indian mythology that refers to severing of the head, it is apparent that severing of the head is a recurring pattern. For instance, the Rig Veda mentions Indra cutting off Dadhyanc’s head. While Shatpath Brahmana narrates how Vishnu’s head was ‘accidently’ cut off when ants gnawed off the strings of a flexed bow on which he was resting his head. The Gods then placed a horse head over Vishnu’s shoulders and named him Hayagriva. Shiva when humiliated by Daksha- his father in law, became furious and severed his head. He later replaced Daksha’s head with a goat head. One can count more than a few examples, that show the severing of human head and replacing it with an animal head is a significant mythological motif. Therefore, one can ascertain that Ganesha’s head being replaced by an elephant head is a deliberate mythological trope.

But what does this pattern symbolize?

Death and rebirth is a persistent motif in sacrificial rituals. It is clearly enacted in the brahmanical rite of passage like Uapanayan, where the young boy initiate has to symbolically pass through the cycle of death and rebirth.
In the upanayana ritual the boy leaves his parental home to go his Guru’s ashram for higher education – but only after he symbolically dies and is reborn as his guru’s child. (This ritual is conducted in a ‘womb hut ‘ or dikshitavimita , where the student ( shishya) is placed doubled up in a fetal position in his guru’s lap). Having performed uapanayana, the boy is considered as dvija or twice born, and this symbolic death is represented by shaving off his hair (rite known as chudakarman) which is equated to the severing of one’s head.

Ganesha is the son of Parvati and not of Shiva. (This is reversed in case of Skanda- Karttikeya who is the son of Shiva, and not of Parvati ) . According to the story- Ganesha took upon himself the role of a loyal guard to Parvati. When Ganesha blocked Shiva’s entry into Parvati’s abode even after learning that Shiva is Parvati’s husband, a battle breaks out and Shiva beheads him. An anguished Parvati reproached Shiva for ‘killing’ her son and Shiva agreed to bring him back to life by attaching a new head to Ganesha’s body. Here Ganesha the son of Parvati dies but is reborn to Shiva as his son, the elephant headed Ganesha. Shiva then establishes Ganesha as the head of his ganas as gananayak. By this process Ganesha completes the cycle of death and rebirth and becomes the son of Shiva and Parvati.

So is there any significance for the elephant head?

Puranic tales provide a variety of accounts to explain this anomaly, including a tale about how Shiva and Parvati assumed the form of elephants and made love and thus giving birth to Ganesha with an elephant head. The most common tale that gained currency is the one in which Shiva severs Ganesha’s head and replaces it with the elephant head. Some tales also ascribe the source of the elephant head to various supernatural elephant entities in Indian mythology, such as Indra’s mount- Airavata, or the demon Gajasura whom Shiva defeated in a battle.
The elephant symbol is well entrenched in the South Asian mythology , and can be traced back to the Mohenjodaro civilization. It symbolizes rain or the water element and is therefore closely linked to agrarian symbols of fertility. Ganesha’s elephant head therefore suggests his primary role as a fertility god. The popular Ganesha festival is primarily a harvest festival, and a celebration of abundance and prosperity. Being a fertility god, sacrifice and initiation are inherent in the character of Ganesha. This again reiterates why severed head motif is central to Ganesha’s identity.