The Socceroos looked on course for their third successive win under new boss Ange Postecoglu when Cahill twice headed home, either side of winning a penalty for skipper Mile Jedinak to convert, in the first half.
Cahill's goals took him clear at the top of Australia's goalscorer chart, with 31 strikes, on the ground in which he first made his name in English football with Millwall.
A 3-0 half-time lead was no more than Australia deserved, but the match turned in two mad minutes shortly after the break.
Fidel Martinez pulled a goal back on 57 minutes before Australia goalkeeper Mitch Langerak, who had only come on at half-time, was sent off after he tripped Enner Valencia after he had been put through on goal.
Referee Lee Probert showed Langerak a red card - after the Borussia Dortmund man's vicious swipe at the ball took man only - and replacement Brad Jones' first act was to pick Segundo Castillo's penalty out of his net.
Jones then almost gifted Ecuador an equaliser, when he was dispossessed needlessly trying to dribble out of goal, and the Socceroos' defensive frailty was exposed in the final 10 minutes.
Enner Valencia bundled home an equaliser on 77 minutes before Edison Mendez struck the winner in injury-time after Australia again tried to pass their way out of defence only to give the ball away under pressure.