Question: "How did Moses write Deuteronomy if it records his death?"
Answer: Most of the book of Deuteronomy is comprised of “farewell speeches” that Moses gave to the children of Israel before they entered the Promised Land. Prior to his death, Moses transcribed his collection of speeches and “gave it to the Levitical priests, who carried the ark of the covenant of the Lord, and to all the elders of Israel” (Deuteronomy 31:9). Moses is identified in the book of Deuteronomy as the author (Deuteronomy 31:9, 24), and Jesus often quoted from Deuteronomy, confirming Mosaic authorship (Matthew 19:8, citing Deuteronomy 24:1–4). However, the final chapter of Deuteronomy prompts the question at hand: who wrote Deuteronomy 34, since that is the record of Moses’ death?
God had told Moses that he would not be able to lead the children of Israel into the Promised Land because of his prior disobedience (Numbers 20:12) and that he would die in Moab. Deuteronomy 34 relates how Moses was allowed to view Canaan from afar before he died (Deuteronomy 34:1–6). After the death of Moses, the leadership of Israel fell to Joshua (Deuteronomy 31:7, 14; Numbers 27:18–23).
Beyond reasonable question, Moses wrote Deuteronomy very near the end of his life. It is likely that Joshua, as Moses’ successor as leader of Israel, wrote the account of Moses’ death. Other theories include Ezra as the author of Deuteronomy 34 or the seventy elders who served under Moses (see Exodus 24:9).