It will depend on the nature of the crime, the level of diplomatic credential the person holds, and the specifics of the hosting agreement between host country and the embassy country.
Ex. the U.S. has some diplomatic postings abroad where some of its people on diplomatic credentials are made subject to local law enforcement for certain crimes, while for others they are not. Usually, the highest-ranking diplomatic officials are essentially immune from any prosecution for any crime committed in the host country, by the host country–they can still be prosecuted by the embassy country, though. There was an episode of the old Columbo series in which a diplomat from an Arab gulf country murders the brother of the King/Emir what have you, Columbo eventually proves it was him, but the diplomat snidely remarks “ah, but as you say, I have diplomatic immunity, so while you have proven your case, you cannot do anything about it.” Cue the Emir walks out from a side room where he had been waiting to hear this conversation. Columbo then tells the murderer he can waive his immunity and face prosecution in California, or he can face prosecution by the Emir at home. The murderer looks aghast and says his home country is “barbaric” and he would not go back there.
Now that’s a Hollywood story–the nucleus of it is true though–just because the diplomat was immune from prosecution in America, he would not be immune from prosecution back in his home country. Now some specifics of Columbo were wrong–diplomatic immunity is a privilege enjoyed by the country not the individual, confusing as it may be. It isn’t actually the diplomat’s immunity to waive, it is the country’s. So technically in that scenario, the murderer would not have the legal option to waive his own immunity, and likely could not prevent his being recalled back to his home country to face trial. Maybe he could beg asylum in the United States if he could demonstrate some innate violation of his human rights was going to occur at home, and essentially “defect.”
Note also there’s no requirement to say, let someone go on a shooting rampage in a host country because they’re a diplomat. The host country’s security forces could respond to that with lethal force. If they captured a diplomat in the midst of a shooting rampage, they couldn’t prosecute him, but they could certainly place him in custody to avoid him doing more harm, declare him persona non grata (that is when a host country revokes immunity–note that it isn’t retroactive, the person then has a time window in which they must leave the country, if not they forfeit treatment as a diplomat), and send his ass home.