If the shelter released the dog as an adoption, then they legally owned the dog at the time that they placed it for adoption and had the right to adopt it out to whoever they chose.
The fact that the dog was microchipped would not make a difference unless the state/local law required that the shelter attempt to track down the registered owner and they failed to do so. If the shelter followed all applicable laws, the shelter was the owner of the dog at the time that they placed it. They could choose to adopt it to the original owner, to the more recent owner, or to someone else altogether.
But if they returned it before they had obtained ownership, that’s a hard question to decide.
I can see why they would choose the most recent long-term owner because it seems like that would be the least stressful option for the dog. I understand why they’d want to release it to the most familiar home with the family it has been with for several years - the dog is likely very attached to its current owners. I’m sure he would get attached to its original owner, too, but there would be a degree of stress when he loses his current owners and has to adjust to new surroundings and new people. And given the circumstances, it makes sense that the shelter would say that the question of ownership at this point would be a civil matter between the two owners.
In any case, the release choice isn’t likely to change now, even if the original owner tried to challenge it in court. I’m very sorry that the original owner is hurting, but I hope that there’s some relief in knowing that the dog is alive and well.