Heading to AAMI Park on Friday night with four wins in five games, and just a point behind second-placed Western United, City had been expected to easily account for the cellar-dwelling Roar.
But they were stunned when Asumah Abubakar beat the offside trap and put the visitors ahead just nine minutes in.
City fought back to seize the lead in the second half through Marco Tilio and Max Caputo but were again left reeling when Keegan Jelacic restored the game to parity with a deflected effort in the 69th minute.
Ruben Zadkovich's side only managed one further attempt all game – City sent in 24, with seven on target – but they looked like that would prove enough for an unlikely point until Ferreyra rose to meet a Yonatan Cohen cross and win the game for City.
It was Cohen's first assist of the season and Ferreyra's first goal since he netted for Russian second-tier side Enisey Krasnoyarsk last May.
The win moved Aurelio Vidmar's side to 43 points and into second on the live ladder, just three behind league-leaders Auckland. Western United, however, can reclaim their spot on the table away to Western Sydney on Sunday.
After Abubakar was played on-side by Aziz Behich, who started at centre back in place of an injured Samuel Souprayen, the Roar bunkered down and increasingly frustrated the hosts.
But a moment of brilliance from Socceroo attacker Tilio broke through the Roar's resistance in the 57th minute and Caputo capitalised on a some chaos to fire home four minutes later.
Jelacic's equaliser again threatened to derail things but Ferreyra had the last word.