The League Two Cobblers went ahead through Matt Warburton, but Ayew was introduced straight after and a second-half brace from the Ghana captain turned the game on its head.
First he levelled the game with a header and after George Byers made it 2-1, Ayew made the game safe as he made his first Liberty Stadium appearance in 15 months.
The home side dominated the first 20 minutes, but could not make the final pass stick as Barrie McKay's weaving run and cross was cleared.
At the other end Cobblers captain Ryan Watson fired over and Warburton shot just wide.
Warburton curled over again at the start of the second half and McKay shot weakly on target.
You got the feeling a Northampton goal was coming and in the 61st minute it arrived.
The Cobblers broke clear down the right and Billy Waters found Warburton who drove the ball into the bottom corner.
Swansea immediately introduced Ghana captain Ayew for his first appearance since May 2018. Northampton's former Swansea goalkeeper David Cornell – who was born just down the road from the Liberty Stadium – then produced great saves from Jordon Garrick and Sam Surridge.
Swansea levelled when Ayew reacted quickest to a loose ball to head past Cornell in the 80th minute.
Byers then found the bottom corner from the edge of the box three minutes later and Ayew completed the scoring two minutes from time.