"And the angel stretched out his hand toward Jerusalem to destroy it, and the Lord said to the angel that destroyed among the people: It is enough" (2 Samuel 24:16). They said: on that day Abishai son of Zeruiah died. "And He said to the angel that destroyed among the people" — "Great (rav) is the pledge that is in your hand." "And Gad came to David [on that day] and said to him: Go up, raise up [to the Lord] an altar," and so forth. To what was David comparable at that hour? To one who was striking his son, and the son did not know on account of what he was striking him. At the last he said to him: Know that I struck you on account of the wrong done to so-and-so. So too, all those multitudes who fell — they fell only because they did not demand the building of the Temple. And behold, these are matters of light and heavy (a fortiori): if these, who did [not] see the building of the Temple, were thus, we — all the more so. Therefore the early prophets ordained that Israel should pray three times each day, and say: Return Your Presence to Zion, and order Your service in Jerusalem Your city.
Why the Plague Fell and the Sages Ordained Prayer Three Times Daily
Midrash Shmuel 31:4
וישלח ידו המלאך (אל) ירושלם לשחתה, ויאמר למלאך המשחית בעם רב (שמואל ב' כ"ד ט"ז), אמרו אותו היום מת אבישי בן צרויה, ויאמר למלאך המשחית בעם רב, רב הוא משכונא דגבך. ויבא גד אל דוד [ביום ההוא] ויאמר לו עלה הקם [לה'] מזבח וגו', למה דוד דומה באותה שעה, לאחד שהיה מכה את בנו, ולא היה יודע בשל מה הוא מכהו, באחרונה אמר לו דע על נקמת פלוני הכיתך, כך כל אותן האוכלוסין שנפלו לא נפלו אלא על ידי שלא תבעו בבנין בית המקדש, והרי דברים קל וחומר, ומה אלו [שלא] ראו בבנין בית המקדש כך, אנו על אחת כמה וכמה, לפיכך התקינו הנביאים הראשונים שיה' ישראל מתפללין שלשה פעמים בכל יום, ואומרים השב שכינתך לציון וסדר עבודתך לירושלם עירך.