Genesis 14:14-16 and Joshua 10:12-14
12 On the day the Lord gave the Amorites over to Israel, Joshua said to the Lord in the presence of Israel:
“Sun, stand still over Gibeon, and you, moon, over the Valley of Aijalon.”
13 So the sun stood still, and the moon stopped, till the nation avenged itself on its enemies,
as it is written in the Book of Jashar.
The sun stopped in the middle of the sky and delayed going down about a full day. 14There has never been a day like it before or since, a day when the Lord listened to a human being. Surely the Lord was fighting for Israel!
Joshua read the Book of Jashar, particularly the part where it mentioned the sun standing still. While fighting the Amorites, under the inspiration of the Holy Spirit, he called the sun to stand still, thereby fulfilling this prophecy in Jashar. It was a sign to show that the Lord was fighting for Israel. That's the reason.
There is also a play in words of Joshua (יְהוֹשׁ֫וּעַ) and Jashar (יָשָׁר).
There is another prophecy in Jashar that David fulfilled in 2 Samuel 1:18.
Why was it important for God to hold the sun in place for Joshua to kill the Amorites as quickly as possible?
To demonstrate that surely the Lord was fighting for Israel.