The reason the war went on longer than it needed to relates to something Churchill said, when he remarked that the "wars of the peoples" would be worse than the "wars of the kings".
If a king in the 16th Century lost a key battle in a war, he could simply say, "Okay, I lost. I'll pay you this many gold pieces and surrender this fortress and that province in exchange for peace." And the other king would say, "Sounds good. Nice doing business with you." And then they both go on being kings and not caring a whit what the people of their kingdoms think.
But the Civil War was a "war of the peoples", to use Churchill's phrase. Both the Union and Confederacy were republics, in that their leaders were elected and, therefore, had to justify their decisions and the consequences of those decisions to their people. Once it became clear that the Confederacy was doomed (take your pick as to when that was), the logical thing for President Davis and the Confederate government to do would be to seek peace on the best terms they could get. Had Davis been a king, perhaps he would have done exactly that. But since he was the elected leader of a republic, he couldn't do that, because it would mean acknowledging to the Southern people that the war had been a terrible mistake and all the men who had died had given their lives in vain. So as long as there was even the slimmest, slightest chance of a victory, they would keep rolling the dice rather than face the music.
The same thing is true of World War I. Once the stalemate was achieved on the Western Front, any rational and reasonable king would have simply said that the war was too much trouble and cost too much and made peace. But the leaders of Britain, France, Germany, and Russia either had elected governments or faced the possibility of revolution, so they lived in fear of retribution from their people if they failed to achieve victory after so much had already been lost. And so the unnecessary and stupid war went on for four years and killed millions and millions of people.