So how did Moses write about his death before the first five books of the Old Testament end?
Joshua was the prophet who came after Moses "Now Joshua the son of Nun was full of the spirit of wisdom, for Moses had laid his hands on him; so the children of Israel heeded him, and did as the LORD had commanded Moses" (Deuteronomy 34:9). It is believed that Joshua wrote the last chapter of Deuteronomy. The writing was placed at the end of Deuteronomy because it was about Moses and made a fitting ending regarding Moses' life.
It is not unusual for a later prophet to finish the writing of a former prophet. Joshua 24:29-33 appear to be written by the prophet who began Judges because it uses similar terms. It too mentions the death of Joshua in the book that was mostly written by Joshua.