In German, Ersatz is a neutral, practical word. A spare part for a car is an Ersatzteil, and Wikipedia notes that this is a technical term with no implication about quality. In English, calling something ersatz is an insult. The change happened in wartime.
Merriam-Webster dates the English word to 1871 and gives its source as the German noun Ersatz, "substitute." Wordsmith.org finds it in 1875. For decades it stayed rare. A Merriam-Webster Word of the Day note says it "really came into prominence during World War I."
The reason was the British naval blockade of Germany. The National WWI Museum and Memorial says that by 1915, shortages worsened by the blockade had brought widespread rationing and substitute goods to Germany. As imports dwindled, ersatz goods became part of daily life. Acorns stood in for coffee and roasted plum leaves for tea. One coffee substitute used roasted oats and barley flavored with coal tar extracts.
Bread and clothing were hit too. German war bread, Kriegsbrot, used rye, potato meal and sugar, with less and less wheat over time. Paper fiber replaced canvas, and some menswear cloth was woven from shredded newspapers. An American sergeant on occupation duty reported buying a paper "ersatz" suitcase for 30 marks, against 650 marks for leather.
The army used the word as well. Wikipedia says that early in the war, replacement troops for battered German infantry units came from less-trained Ersatz Corps.
Officials claimed the substitutes were as good as the real thing, the museum notes. English took the word along with that experience, and it has meant "inferior imitation" ever since.
World War II renewed the word's English life. Merriam-Webster notes "a resurgence of the word" then. Wikipedia credits Allied prisoners of war, mostly airmen, with popularizing it in Britain after their German captors served them Ersatzkaffee, a grain coffee.
Today Merriam-Webster defines ersatz as "being a usually artificial and inferior substitute or imitation." A German mechanic ordering an Ersatzteil means nothing of the kind.