Al Ahly secured third place in the FIFA Club World Cup after a dramatic penalty shootout win over Palmeiras.