Ayew had put Swansea ahead in the first half when scoring for the third consecutive game, before Sam Morsy levelled in the first minute of injury time.
But Jake Bidwell then tumbled under George Saville’s challenge in the penalty area and, although referee Gavin Ward at first pointed for a corner, a spot-kick was awarded and Ayew coolly stroked home.
The decision left Boro boss Neil Warnock fuming and the veteran manager was booked for his protests.
Middlesbrough will feel they deserved more after pushing Swansea back and dominating possession and territory for long periods.
But Boro created few attacking opportunities for all their promise and were unable to celebrate at the end of a week when Warnock extended his stay on Teesside for next season.
Swansea omitted Jamal Lowe after the striker’s 10-game goalless run and Conor Hourihane returned to the starting line-up in a more advanced position.
Middlesbrough were forced into an early change as Anfernee Dijksteel’s second collision with Ryan Manning in the space of a few minutes saw him make way for Djed Spence.
Despite winning late at Stoke on Wednesday, Swansea’s form has dipped slightly in recent weeks and Ayew’s overhead attempt was the only time the Boro goal was threatened during the opening 40 minutes.
Boro grew in confidence throughout the first period and Marc Bola’s break to the byline was almost rewarded with the opening goal. Bola crossed and Duncan Watmore turned it goalwards only for Marc Guehi to intervene with a vital touch that sent the ball wide.
Yannick Bolasie tried his luck from distance but his effort was comfortably gathered by Freddie Woodman in the Swansea goal.
Bolasie was booked for hauling down Guehi and the foul led to Swansea taking the lead.
Hourihane’s free-kick was only cleared as far as Connor Roberts and his return header prompted confusion in the Boro defence.
Bola and Jonny Howson went for the same ball and it fell kindly to Ayew, whose shot deflected off Spence and beat Marcus Bettinelli at the near post.
Boro had the ball in the Swansea net within seconds of the restart as Bola drove home ferociously from 25 yards – but referee Ward had already penalised Bolasie for clattering into Jay Fulton inside the penalty area, much to the obvious frustration of Warnock and his lieutenants.
Middlesbrough continued to press, with Swansea struggling to get out of their own half and having to defend several dangerous crosses into their box.
Saville headed wide from one of them and Swansea sent on Lowe for Korey Smith to give them an attacking outlet and ease the pressure.
But Boro levelled when Morsy slammed home his first goal since February 2020, although Ayew’s 13th of the season in the seventh minute of stoppage time ultimately denied them with his second late spot-kick winner of the week.
Support The Yorkshire Post and become a subscriber today. Your subscription will help us to continue to bring quality news to the people of Yorkshire. In return, you’ll see fewer ads on site, get free access to our app and receive exclusive members-only offers. Click HERE to subscribe.