But the former deputy prime minister, who arrived alongside the chief justice, found herself barred from the building, which had been locked by allies of Mr Malielegaoi in advance of her arrival.
Instead, Ms Mata’afa and members of her Faatuatua i le Atua Samoa ua Tasi (Fast) party gathered in a marquee in parliament’s gardens, in the capital Apia, with supporters looking on, and were sworn in one by one.
In a statement, the Fast party said: “Democracy must prevail, always. There can be no exceptions from this fundamental principle. Those who claim otherwise and act accordingly play with fire.”
The ad-hoc ceremony was rejected by rivals as unofficial. Mr Malielegaoi called the improvised swearing-in ceremony “illegal and unlawful”.
The controversy comes a month after the closest-run general election in Samoa’s history, which was followed by bitter disputes and legal challenges.
Mr Malielegaoi’s Human Rights Protection Party (HRPP) was ousted from power after four decades by Ms Mata’afa’s insurgent Fast party. Both parties won 25 seats, but a single independent MP broke the tie in favour of the Fast.
That led to legal manoeuvring by the HRPP, which claimed its opponents had not correctly met the quota of female MPs. Samoa’s election commission revoked the results of the April vote and called a fresh election for 21 May.
But five days ahead of the re-run, the supreme court ruled against the HRPP, re-endorsing the results of the election and ordering the swearing in of Ms Mata’afa to go ahead.
After 22 years in charge, Mr Malielegaoi is the world’s second longest-serving prime minister.
After serving as Polynesia’s first female deputy prime minister, Ms Mata’afa success in the general election makes her only the second woman in the region to head a government.
She is the daughter of the country’s first prime minister, and has been politically active since the mid-1980s.