The people of the pre-Islamic era used to fast on the day of Ashura, and the Messenger of Allah ﷺ and the Muslims fasted on it before the fast of Ramadan was made obligatory. When Ramadan was made obligatory, the Messenger of Allah ﷺ said, "Ashura is one of the days of Allah, so whoever wishes may fast it, and whoever wishes may leave it."
أن أهل الجاهلية كانوا يصومون يوم عاشوراء، وأن رسول الله ﷺ صامه والمسلمون، قبل أن يفترض رمضان، فلما افترض رمضان، قال رسول الله ﷺ: إن عاشوراء يوم من أيام الله، فمن شاء صامه ومن شاء تركه.
This was narrated by Bukhari in the tafsir (4501), and by Muslim in fasting (1126/117), both through the route of Ubaydullah (he is Ibn Umar Umari), from Nafi, who said Abdullah bin Umar informed me, and he related it. The wording given is Muslim's.His saying, "The people of the Jahiliyyah used to fast on the day of Ashura," means that they had no knowledge of the reason for this fast, for they had received it in this form from an earlier religious law.Ikrima was asked about this, and he said, "Quraysh committed a sin during the Jahiliyyah, and it weighed heavily on their hearts, so they were told, 'Fast on Ashura and it will atone for that.'" This is what he said, or something to that effect.And it is said that Allah Most High honored ten of the prophets, peace be upon them, on this day with ten distinctions.First, Musa, for he was granted victory on this day, the sea was split for him, and Pharaoh and his soldiers were drowned.Second, Nuh, for his ship came to rest on Mount Judi on this day.Third, Yunus, for he was saved from the belly of the whale on this day.Fourth, it was on this day that Allah accepted the repentance of Adam, as Ikrima said.Fifth, Yusuf, for he was brought out of the well on this day.Sixth, Isa, for he was born on this day and was also raised up to heaven on it.Seventh, Dawud, for Allah accepted his repentance on this day.Eighth, Ibrahim, for he was born on this day.Ninth, Yaqub, for his eyesight was restored to him on this day.Tenth, our Prophet Muhammad ﷺ, for on this day he was forgiven for whatever sins came before and whatever would come after.Umdat al-Qari (117/11).Whatever the reasons may be, it is established that the Prophet ﷺ used to fast on Ashura before he migrated to Madinah, and he continued to fast it and commanded that it be fasted after his migration to Madinah, until the fasting of Ramadan was made obligatory in the second year. He said, "Ashura is one of the days of Allah, so whoever wishes may fast it, and whoever wishes may leave it."When the Prophet ﷺ learned, in the eleventh year before his death, that the Jews also held this day in great esteem, he said, "If we live until next year, Allah willing, we shall fast on the ninth day as well." And in another narration, "If I remain until next year, I will surely fast on the ninth."That is, as soon as the Prophet ﷺ learned that the Jews fasted this day and held it in great esteem, he asked them about it, and they said, "Allah saved Musa and his people on this day, and drowned Pharaoh and his people, so Musa fasted it in gratitude, and we fast it too." The Messenger of Allah ﷺ then said, "We have more right to Musa than you do." So he fasted it, and commanded that it be fasted, and that they be opposed by adding the ninth day.