Prosecutors said Farhad N., who was born in Kabul, shouted “Allahu Akbar” after the attack and believed he was obligated to kill randomly selected people in Germany in response to the suffering of Muslims in Islamic countries.