The investigation will determine how problems relating to ventilation and other key building systems occurred and what steps can be taken to prevent the same situation happening in future projects.
The announcement comes after mounting pressure on the Scottish health secretary to address serious failings at the new hospitals, which have led to patients deaths and wards being closed due to infection risks.
A child and another patient died at the QEUH in Glasgow after picking up an infection related to pigeon excrement and there have been several patients affected since then.
Edinburgh’s £150 million Royal Hospital for Children and Young People (RHCYP) was originally due to open in 2017 but has been hit by repeated delays due to safety concerns.
The opening date was again put back from this July when last-minute inspections found serious problems with the ventilation system in the critical care unit. The facility, which has the same building contractor as the Glasgow super-hospital, will now not be ready until next autumn at the earliest.
Pressure has been building on the Scottish Government to investigate the issues, with families of patients and politicians calling for an inquiry despite a number of reports.
“The safety and well-being of all patients and their families is my top priority and should be the primary consideration in all NHS construction projects,” health secretary Jeane Freeman said.
“I want to make sure this is the case for all future projects, which is why, following calls from affected parents, I am announcing a public inquiry to examine the new RHCYP and the QEUH sites.
“The recent KPMG and NSS reports into the new Edinburgh children’s hospital will provide a significant amount of the underpinning evidence for the inquiry alongside the ongoing independent review into the delivery and maintenance of the QEUH.
“The current situation is not one anyone would choose – but it is one I am determined to resolve.”
A report published earlier this month said upwards of £16 m of repairs were needed to fix the ventilation and other issues that had been identified to make the RHCYP safe, with work expected to take at least 12 months.
This comes on top of the cost of continuing to run the existing hospital site.
Scottish Conservative shadow health secretary Miles Briggs said: “This announcement is long overdue and it’s only come about because the SNP hierarchy has become fed up of negative headlines.
“The ongoing problems at both hospitals are consequences of SNP negligence of the NHS, which has gone on for more than a decade.
“It is now vital that the public inquiry reports as soon as possible and considers the decisions taken around these projects by all four SNP health secretaries.”
Scottish Liberal Democrat health spokesman Alex Cole-Hamilton said the inquiry is “a welcome but long overdue U-turn” from the health secretary, accusing the government of “excuses and evasion”.
He said: “The series of incidents at the Queen Elizabeth University hospital have been of the gravest magnitude, while the Sick Kids hospital has racked up bill after bill and delay and delay.
“Quite simply, we cannot have young patients being treated in facilities that are not up to scratch.”
He said the inquiry must now move forward in a way that does not further hold up the opening of the children’s hospital.
Scottish Labour health spokeswoman Monica Lennon said: “A public inquiry is the only way to get to the bottom of this outrageous series of errors, which has seriously disrupted patient care and cost taxpayers millions of pounds.
“It should not have taken weeks of pressure from Scottish Labour, patients and families for this to have been agreed to by the health secretary.
“Children in Scotland are being let down because the hospitals they were promised are not fit for purpose.”
Scottish Greens health spokeswoman Alison Johnstone also welcomed the inquiry, urging the health secretary not to limit its scope.
She added: “The Scottish Government must ensure that the health and well-being of patients and staff is paramount going forward, and provide adequate support to all those who have been affected at both sites.”