Abdullah bin Buraydah (may Allah have mercy on him) narrates from his father (Sayyiduna Buraydah, may Allah be pleased with him) that he said: I was sitting with the Prophet (peace and blessings be upon him) when a woman came and said: O Messenger of Allah! I gave my mother a slave-girl in charity, but my mother has died. He (peace and blessings be upon him) said: "Your reward is assured, and the inheritance has returned her to you." Then she said: O Messenger of Allah! She also owed a month of fasting; shall I fast on her behalf? He (peace and blessings be upon him) said: "Fast." Then she said: O Messenger of Allah! She did not perform Hajj; shall I perform Hajj on her behalf? He (peace and blessings be upon him) said: "Perform Hajj on her behalf."
Sulayman bin Buraydah narrates from his father: a month of fasting. So these hadiths establish the permissibility of fasting on behalf of the deceased. Imam al-Shafi'i (may Allah have mercy on him) said in his old book: Something has been narrated about fasting on behalf of the deceased; if it is established, fasting is done on his behalf just as Hajj is performed on his behalf. As for the new book, he raised the question himself and said: If it is said: It has been narrated that the Messenger of Allah (peace and blessings be upon him) ordered someone to fast on behalf of another, the reply is: Yes, Ibn Abbas narrated it from the Prophet (peace and blessings be upon him). If it is said: Then why do you not adopt it? The reply is: Al-Zuhri narrated from Ubaydullah, from Ibn Abbas, from the Prophet (peace and blessings be upon him) "a vow" without specifying it, despite al-Zuhri's precise memory and Ubaydullah's long companionship with Ibn Abbas; so when someone else came, from a man, from Ibn Abbas, with something different from what is in Ubaydullah's hadith, it is more likely that it is not preserved.
Hadith Referenceالسنن الكبرى للبيهقي / كتاب الصيام / 8231