The 22-year-old becomes City's third signing of the summer and will join the club on July 1 on a free transfer after his Old Trafford contract ends.
Macheda worked with Cardiff boss Ole Gunnar Solskjaer previously at United.
Macheda told Cardiff's official website: "It's a massive opportunity for me and it is with thanks to Ole Gunnar Solskjaer that I am here."
Cardiff announced the signing on their website on Tuesday after the Italian striker successfully completed a medical.
Macheda said: "Last season I had a good spell on loan at Birmingham City, but this is a new chapter of my career now and I can't wait to get started and show everyone who I am.
"Knowing Ole, he wants to get back into the Premier League as soon as possible and the recent signings show that.
"I'm here for the same reason. I feel that this is a great team and if we can have a good season and work hard then we can go straight back up."
The Italy Under-21 international joined the Manchester United youth set-up from Lazio in 2007, going on to top the Under-18 scoring charts in his first season at Old Trafford, netting 12 goals in 21 appearances.
Macheda burst into the spotlight as a 17-year-old when he came off the bench against Aston Villa in 2009.
United were 2-1 down at the time before Cristiano Ronaldo grabbed an equaliser and Macheda curled home a debut goal in stoppage time to win the match for Sir Alex Ferguson's title-challenging side.
Macheda continued to feature in the United squad that season, scoring his second goal for the club against Sunderland just 46 seconds after taking the field to replace Dimitar Berbatov.
He was rewarded with his first Premier League start for United against Middlesbrough that season.
That campaign saw him win United's Jimmy Murphy Academy Player of the Year award.
A Champions League debut against CSKA Moscow followed the next season before injury started to hinder his progress.
The following few years saw him on loan away from Old Trafford - including spells at Sampdoria, Queens Park Rangers, VfB Stuttgart, Doncaster Rovers and Birmingham City, where he scored 10 goals in 18 appearances.