Mohamed Salah opened the scoring when he lobbed David De Gea after connecting on Roberto Firmino's pass.
Mason Greenwood and Marcus Rashford put United ahead before Salah scored his second to square the score.
But United found a winner through substitute Bruno Fernandes.
The Portuguese came off the bench to fire in a free-kick past Alisson, booking a meeting with West Ham United in the next round.
And manager Ole Gunnar Solskjaer was delighted with his team's performance.
"Brilliant - you know when you go a goal, down the reaction of everyone was really good," said Solskjaer.
"We played some really good stuff, good goals, we have to defend well against them and we managed to react well and of course towards the end there were some moments but we kept them away."