Battlements were first used in Egypt. The earliest known example can be found in the palace of Medinet-Abu in Thebes. It’s believed that this architectural design derives from Assyrian fortresses. Bass relieves from Nirum shows them around Assyrian towns, and there are traces found also in Mycenae in Greece. The Great Wall of China also has battlements.
Romans used wooden pinnacles on top of their first aggeres. Internal buttresses were used in Pompeii to provide protection on one side of a wall.
In the Middle Ages, battlements had arrow loops of various shapes depending on the weapon to be fired. From the 13th century, merlons were connected with wooden shutters that could be opened temporarily to allow fire against attackers. Crenallations (or openings between the two raised solid portions of merlons) provided cover for those firing weapons and allowed to get very close to the wall face.