I've read about several instances where Nazi officers knew that if they lost the war, they would be tried and executed for what they did. Sometimes trying to cover up their crimes or help someone out to get an alibi.
So how did they know they would be in trouble? Was there a precedent that had been set before the war that they would be tried and punished for what they did?
Well, the hardcore Nazis believed that if Germany lost the war it would be the end of the German people. The Jews would manipulate the world powers to destroy the German people or the Slavic races would degenerate the German people by destroying their racial purity and their culture. Their world view was one where there was a major conspiracy to keep Germany down existed and the only resistance possible was through warfare, meaning that if the Germans were beaten in the field, this conspiracy would ensure that Germany be removed as a threat.
Then there were the anticommunists who may not have truly believed in the Jewish/slav/racialist conspiracies but knew that the Communists in the East could not and would not live side by side with a strong Germany. For the anticommunists they knew there would be trouble for Germany after the war because the Communists would use any means at their disposal to ensure that their politics would be those who would govern the future of humanity. For the anticommunists, the trouble in the after-war was in the fact that Germany had been a bastion against communism and that it now faced a future where the world's communist power would come into their lands as military conquerors.
Then there were the German military aristocrats, the officer class, who knew that Germany in its conduct of the war had committed war-crimes that the Allies would not ignore. They were committed soldiers who fought for Germany because that's what a good soldier must do. It was their duty. They may not have orchestrated the war-crimes but many followed orders passed down by those who did. They knew that the victors would not ignore that.
Doenitz and Kesselring go into quite a bit of detail in their respective memoirs about the vision the officer class had of the post war era for Germany. Keitel's memoirs are of considerably lesser quality but even he touches on the post-war. Doenitz and Kesselring I would class solidly in the officer class while Keitel was a true believer in the NSDAP. All three however, point to most officers having concerns about all three pictures painted above only to varying degrees.
If you want to know more, I learned everything I wrote in this post from the memoirs of Doenitz, Kesselring and Keitel along with additional details from The Fall of Berlin - 1945 by Anthony Beevor.
Simply because the Allies had made clear to the Germans that atrocities would be punished.
The Moscow Declaration of October 1943 made clear an intention to punish Germans for their actions in occupied Europe.
Let those who have hitherto not imbrued their hands with innocent blood beware lest they join the ranks of the guilty, for most assuredly the three Allied powers will pursue them to the uttermost ends of the earth and will deliver them to their accusors in order that justice may be done.
Apart from the other good answers, German officers generally were probably aware of the Katyn Massacre, which explicitly targeted captured Polish officers as well as other members of the intelligentsia, presumably as nuclei around which Polish resistance to Soviet rule might grow.