Spain caught fire in the fourth quarter to beat Iran at the FIBA World Cup and secure top spot in Group C.