The origins of Elul as a month of special Divine grace and mercy go back to the time of Moses, in the year 2448 from creation (1313 BCE)--the first year after the Jewish people went out of Egypt.
Seven weeks after the exodus, the people of Israel received the Torah at Mount Sinai and entered into an eternal covenant with G-d as His chosen people. But just 40 days later, while Moses was still up on the mountain, they violated their special relationship with G-d by worshipping a golden calf. Upon descending from the mountain and witnessing their transgression, Moses smashed the two stone tablets on which G-d had inscribed the Ten Commandments; he then retuned to Mount Sinai for a second 40 days to plead with G-d on Israel's behalf.
On the early morning of the 1st of Elul, Moses once again ascended Mount Sinai, taking with him the stone tablets he had hewn, by divine command, for G-d to re-inscribe the Ten Commandments. On the mountain, G-d allowed Moses to "see My back, but not My face" (which Maimonides interprets as a perception of
G-d's reality but not His essence) -- the closest any human being ever came to knowing G-d -- and taught him the secret of His "Thirteen Attributes of Mercy" (Exodus 33:18-34:8).
For the third time, Moses remained on the mountain for 40 days, from the 1st of Elul until the 10th of Tishrei (Yom Kippur), during which time He obtained G-d's whole-hearted forgiveness and reconciliation with the people of Israel. Ever since, the month of Elul serves as the "month of
Divine mercy and forgiveness."
(Also see: The 120-Day Version of the Human Story)