From Sahih Muslim (Hadith 2188):
Abu Huraira reported Allah's Messenger as saying:Hasten at a funeral, for if (the dead person) is good, you would (soon) bring him close to the good. And if it is otherwise, it is an evil of which you are ridding yourselves
Reference: Muslim 2188