They both offered a sacrifice. One was accepted and the other was not because Allah only accepts what is pure.
(Relate to them in truth ˹O Prophet˺ the story of Adam’s two sons—how each offered a sacrifice: Abel’s offering was accepted while Cain’s was not. So Cain threatened, “I will kill you!” His brother replied, “Allah only accepts ˹the offering˺ of the sincerely devout.) [Al-Ma’idah 5: 27]
عندما قدم كل منهما قربانا فتقبل من أحدهما ولم يتقبل من الآخر.(وَٱتۡلُ عَلَيۡہِمۡ نَبَأَ ٱبۡنَىۡ ءَادَمَ بِٱلۡحَقِّ إِذۡ قَرَّبَا قُرۡبَانً۬ا فَتُقُبِّلَ مِنۡ أَحَدِهِمَا وَلَمۡ يُتَقَبَّلۡ مِنَ ٱلۡأَخَرِ قَالَ لَأَقۡتُلَنَّكَۖ قَالَ إِنَّمَا يَتَقَبَّلُ ٱللَّهُ مِنَ ٱلۡمُتَّقِينَ) ]المائدة 5: 27[