The Machine faked Root's death, knowing it was the only way to protect Root and motivate Finch to do what was needed to defeat Samaritan.
The Machine is unable to see the sniper because Samaritan's operatives work in the shadows, but sees Root get shot and sends the police to their location, as it is the fastest way to get medical attention for Root and Finch into custody where Samaritan would be less likely to have another opportunity to target him.
Root is then taken to hospital where Dr Madeleine Enright performs surgery to save her. The Machine summoned her because She needed a skilled surgeon who was trustworthy and wouldn't ask too many questions, and it is mentioned in 4x13 that Dr Enright is among the past numbers who have been helping the team ("Dr. Enright is keeping an eye on Delia at the safe house.").
Once Root is stable, this is the point where the Machine decides to fake her death. As she is injured, Root wouldn't be able to defend herself when Samaritan comes to finish the job, so her only chance is for the Machine to make sure they aren't looking for her. Or rather, they aren't looking for her alive.
Dr Enright injects her with aconite (like Shaw in 2x16) to make her appear deceased long enough to be pronounced dead and for Fusco to identify her body as a Jane Doe, then revives her with atropine.
Meanwhile, the Machine had also summoned Dr Farouk Madani, another previous ally of the team who used to work in a morgue and still owes Finch a favor as mentioned in 3x05. He locates a body matching Root's description and handles the necessary paperwork. And Dr Carr, the otolaryngologist who gave Root her cochlear implant in 3x17, removes her implant and puts it in the new body (more on this later), which is then sent to be buried in Root's place.
During the chaos of inmates being freed at the prison, Daizo, Jason and Daniel are able to get Root out of the hospital and go into hiding. The Machine chose them for this task because they were shielded from Samaritan at the end of season 3, giving them more room to maneuver freely, and Root already knows and trusts them.
The reason the Machine doesn't tell the team about faking Root's death is because Samaritan has eyes everywhere and She can't risk them finding out. She also heard Finch and Root's conversations about him needing to take a more active role in the fight against Samaritan and all the people he's lost, and knows that having him believe he's lost another friend is the only way to push him into doing what it takes to win the war.
The Machine then chooses to use Root's voice for multiple reasons: 1) to sell the story that Root is really dead. 2) She needs to be able to communicate clearly and quickly so Her usual spliced-together recordings wouldn't work. 3) This is the first time She will have prolonged contact with someone other than Root and She is on her own, so using Root's voice is a way to have part of Her interface with Her still.
When the Machine calls Finch from the hospital, he asks, "Root?". The Machine replies, "No, Harold. I chose a voice." She does not tell him Root is dead, but rather allows him to come to that conclusion based on the state in which he last saw her and the fact that She is now using her voice.
When the Machine talks to Finch about Root being dead, this is the only time She uses a name Root no longer identifies with. Therefore, when She says "I watched her die 12,483 times in the seconds before she expired. I couldn't save her, but I kept trying.", She is actually talking about accessing old footage of young!Root, watching her become the Root we know and running simulations to see whether things could have happened differently ("You taught me to see everything, see everyone, and I do. But I see thousands of versions of them: what they were, what they are, what they could be."). The Machine couldn't save her because this happened before She was built.
Much like how She went behind the team's back to protect Root by faking her death with the help of past numbers, in this episode the Machine goes behind the team's back to protect Reese by giving his number to the DC Team made up of past numbers.
The Machine says, "You know I can't lie to you, Harold." As we've seen, She can withhold information or choose to phrase things a particular way, but can't say an outright lie. At no point does she ever explicitly say Root is dead.
The Machine also says that in a world where She didn't exist, Shaw "never would have met Root." Finch adds, "Or lost her." The Machine does not address this comment at all.
When Shaw visits Root's grave and discovers the soil disturbed, the Machine comments that it "looks like they dug her up. Guessing they got to her cochlear implant. I should have had her cremated, but I just didn't have the heart. My bad." The Machine is only stating what it 'looks like', not what actually happened. And it is very suspicious that the Machine, who was created to predict human behavior and watched Samaritan try to cut the cochlear implant out of Root's head while she was alive, would fail to predict them going grave robbing for it.
Unless, after deciding to fake Root's death, She knew they wouldn't stop until they found it so lets them have it on Her terms. (As previously mentioned, the cochlear implant had been inserted into a body matching Root's description that was buried in her place. Having her cremated, and by extension destroying the cochlear implant beyond use, would have prevented an innocent person's body from being desecrated, but She couldn't give up a chance to defeat Samaritan.)
Once Samaritan scans the implant to access the location data, the Machine is also able to access more information on Samaritan which She then integrates into the copy of Her core code made by Shaw. This, along with the greater defensive capabilities created by Root and activated by Finch, allows her to beat Samaritan in the satellite, when She couldn't win any of the simulated fights.
The Machine knew that this would lead them to the team's frequent locations, so She arranges to protect them by stationing a sniper near the precinct ("It seems Thornhill Industries has been on a hiring spree.") and instructing Shaw to blow a hole in the wall of the subway station that allows them to escape.
Later, when Shaw asks "Is this the guy who killed you? Killed her, I mean.", the Machine changes the subject. This is because She can't lie and couldn't risk telling the truth when Samaritan might find out. Although they don't have much time, it wouldn't have taken long to confirm what Shaw already suspects and she has never been the type to let a desire for revenge interfere with completing the mission since "a good soldier does both" (2x16).
When the Machine directs Finch onto the rooftop, he asks, "Wait, are you sure this is the right place?" and She replies, "Yes. This is where you're supposed to be." It is then revealed that She secretly made a deal with Reese allowing him to sacrifice himself in Finch's place. This shows once again that She will go behind Her assets' backs to protect them and uses careful phrasing to misdirect them without lying.
After defeating Samaritan, the duplicate Machine downloads Herself to the subway station computers and declares 'mission unknown. Awaiting instructions…'. The message left by Her predecessor then begins playing and tells Her about Her assets ("let me tell you who we were") and Her mission ("let me tell you who you are").
As the next scene is Her calling the payphone by Shaw, She must have been given instructions to contact any surviving assets. Shaw answers and it is unlikely the Machine gives her a new number, because She had only just gotten back online and the last time She was reduced to Her core code in order to fit in the briefcase, it took Her a while to start operating normally. We also see that She only begins to scan the crowd after Shaw hangs up.
It can't be Her signature 'can you hear me?' because Shaw doesn't reply. Instead, it must be a message Her predecessor told Her to relay - that She survived, Samaritan is gone for good and it's time to get back to work, followed by a set of coordinates that lead to Root's location as she is both a surviving asset and someone who can help Her get back up and running.
It is doubtful the Machine tells Shaw Root is alive in that moment, because she would have reacted with more shock and betrayal than we saw, and likely demanded an explanation immediately. Instead, she is smiling because they won the war and she can continue saving people.
Shaw then goes to the location, which is a new safehouse where Daizo, Jason and Daniel have been looking after Root, who is still unconscious but recovering. The building has been set up as their new base of operations, since the subway was somewhat destroyed during the final battle.
When Root soon wakes up, she and Shaw are very happy to be reunited and promise never to leave each other again. The Machine moves Herself to the computer equipment in the new safehouse and Root helps Her get recalibrated, as we saw in 5x02 and the flashbacks in 4x05. "There we go. Now, can you see me? Excellent. Next question, who am I? That's my girl." Root becomes both Admin and Analog Interface and Shaw is the Primary Asset.
Together, Root, Shaw, the Machine and Bear get back to working the numbers, with help from their friends and allies.