However, they were opened up after 20 minutes by Connor Roberts who drew a foul from Fabian Delph in the penalty area and Matt Grimes converted from the spot to give Swansea the lead.
Then Bernardo Silva pulled one back, a most generous penalty gifted them an equaliser, and Sergio Aguero scored an offside late victor.
Aguero missed a glorious chance to score but the Argentine striker had the final say when he headed the victor two minutes from time from what looked an offside position. "When we analyse the game we deserved to win by far". "Hopefully next season this won't happen", City boss Pep Guardiola told BBC Sport.
After the break, City were struggling to find a way back into the game and it was after the introduction of Raheem Sterling and Sergio Aguero from the bench that they started to threaten.
Last season, City were embarrassed by League One Wigan in this competition in the fifth round and this was a game which had a similar feel from the early stages.
Many dead in New Zealand shooting at two mosques during Friday prayers
The 1 NEWS broadcaster added that one man arrested by the police had been apprehended outside one of the local high schools. Calderdale leaders have offered their condolences and support in the wake of a shootings at mosques in New Zealand.
England v Scotland, Six Nations 2019
Conversions: Owen Farrell (4), Ford. "There is proper reasoning behind it". "I'm sure Joe won't be sad". I've got him on the right track. "You expose them, bring them back, expose them, bring them back".
Federer and Nadal roll into quarters at Indian Wells
But he couldn't overcome Nadal , who lost his serve just once in the 1½-hour match in sunny and windy conditions. He will next take on 13th-ranked Russian Karen Khachanov , who beat world No. 9 John Isner 6-4, 7-6 (7/1).
Substitute Aguero beat Swansea keeper Kristoffer Nordfeldt with a diving header off Bernardo Silva's low cross at the end of a superb move in the 88th minute.
"We try to play that way", Swansea manager Graham Potter said of the second goal.
For a long time, the story of the game looked like it would be all about Celina, a player who joined Swansea from Manchester City and someone Guardiola knows all about.
"They ask all the clubs about the VAR and all around Europe, the VAR is there and here it's not".
Replays showed the Argentina striker was marginally offside but the video assistant review system was not in use at the Liberty Stadium, despite having been used in City's third and fourth-round ties at their Etihad Stadium home.