In 17:12, the Torah says that Moshe raised his hands and left them up until sunset. וידי משה כבדים ויקחו אבן וישימו תחתיו…ויהי ידיו אמונה עד בא השמש, “Moshe’s hands were heavy and they placed a stone under him…and his hands were faithful until sunset.”
Rashi (based on Rosh Hashana 29a) explains why Moshe’s hands needed to be raised. He states that Moshe’s hands were directed up towards Heaven in intense and deep prayer. A question emerges, though, to which Rashi does not give an answer. Why does Moshe keep his hands up until sunset? He could have prayed to Hashem for just a short time and He would have heard him! Also, why did he specifically wait until sunset and not earlier?
The Rambam writes in Hilchot Taaniot that another prayer service should be recited after Mincha close to sunset on fast days. This prayer service is called נעילה, closing, as if to say, “The gates of mercy are closing down as the sun sets and disappears.”
According to the Rambam, this נעילה prayer, added on special fast days during times of trouble, can only be said at or close to sunset because this is the time at which the “gates of mercy” in Heaven symbolically close. The battle with Amalek was definitely a time of trouble and that day was probably a fast day, dedicated to repentance and prayer. Since Moshe had spread his hands in true prayer for mercy, the most appropriate time to do so would be just before sunset in the time allotted for the נעילה prayer. Therefore his hands were raised until sunset. (Brisk on Chumash.)