Pre-Season Friendly: Brentford 3 Cardiff City 2
The striker's 88-minute goal, which came courtesy of a rebound, was just enough to defeat the Premier League side after Conor McAleny also netted a brace on his Brentford debut.
The 20-year-old, who has joined from Everton on a loan set to last until January, troubled Cardiff from the off and cancelled out efforts from Kim Bo-Kyung and Fraizer Campbell.
Mackay fielded a strong team including Craig Bellamy, Mark Hudson and Craig Noone, but Cardiff could not hold off the determined League One outfit, who showed great resolve to battle from 2-0 down to earn victory in the pre-season friendly.
Cardiff started strongly, however, as South Korea international Bo-Kyung headed home after 12 minutes following a tidy interchange with Campbell.
Campbell got in on the act two minutes later as he netted from 12 yards to double his side's advantage.
McAleny, though, handed Brentford a lifeline as he smashed a shot past Joe Lewis to see the score delicately poised at 2-1 at the half-time interval.
No changes were made for either side during the break, with McAleny on the scoresheet again within five minutes of the restart, hammering another well-drilled effort past Lewis.
The momentum generally seemed to drain from that point, though, and despite nine changes for the visitors, Cardiff simply could not get that winning goal.
Brentford, on the other hand, did manage to sneak that important third as 29-year-old Hayes was on hand to take the rebound past Lewis late on.
After finishing 1-1 against League Two outfit Cheltenham Town last weekend, the Welsh side will be hoping to get a winning streak going when they face Chievo at home on Saturday.