A West African migrant entered the Spanish territory of Ceuta from Morocco curled up next to a car engine and another was hidden behind the back seat of the Mercedes-300 car.
Spanish police found the pair late on Sunday during a customs check at El Tarajal, near the Moroccan border.
The two men from Guinea got first aid, having suffered from a lack of oxygen, Spain's Guardia Civil police said.
Sub-Saharan Africans often try to enter Ceuta, as a gateway to the EU.
The Mercedes 300 was found to have false number plates. Police detained two Moroccan men who were in the car when it was stopped.
It is not clear how long the man in the engine compartment had been inhaling noxious fumes for.
The two illegal migrants did not have any papers, and were identified in the statement only as "TD and AB from Guinea Conakry". They are also under official investigation now.
Ceuta and Melilla are tiny Spanish enclaves on the North African coast, which have erected formidable border fences to keep out illegal migrants.
The fences have led some migrants to resort to extreme methods to get into Spain, such as swimming into the ports or even hiding in suitcases.
Last month, a Moroccan man suffocated to death in a suitcase after his brother tried to smuggle him to mainland Spain aboard a ferry from Melilla.