Wales will become the first country in the world where sprinklers will be compulsory in all new homes.
The law will apply to newly built houses and blocks of flats, as well as care homes and university halls of residence.
It was passed unanimously by Welsh assembly members after being proposed by Labour backbencher Ann Jones.
She said the legislation would save lives and showed how devolution could make a positive difference.
Law-making powers were drawn down from Westminster for the legislation.
Appealing for AMs' support in the Senedd on Wednesday, Vale of Clwyd AM Ms Jones said: "The times I have had to say to people a sprinkler system will not go off unless there is a fire at 68C.
"It won't go off if you burn your toast. It won't go off if the pizza's burning. It will only go off when there is a fire and when it goes off that saves those lives.
"It saves the lives of the people, it saves the lives of firefighters having to go into burning buildings and it just makes sense to get on and do it now."
She added that sprinklers had been in existence since 1885.
The building industry had its chance to fit sprinklers in all new homes, but it was now time to legislate "so we can make people's lives in Wales a lot safer", she said.
Speaking after the legislation was passed, she said: "Not many of the votes we pass here save lives - that one did."
The measure was backed by Wales' three fire and rescue services, the Chief Fire Officers Association (UK), the Fire Brigades Union and many sprinkler campaigners, including the National Fire Sprinkler Network.
The National House Building Council (NHBC) said it had a neutral position on the policy, but raised some questions during the consultation process regarding cost effectiveness and whether existing water pressures would enable sprinklers to work in many homes.