Correction: An earlier version of this story incorrectly identified the Kairaouine Mosque as the largest in Africa. It is one of the largest, but not the largest. The error has been corrected below.

Jemaa el-Fna square in Marrakesh comes alive at night with vendors yelling at passersby to sit down and try their specialties. (Ken Geiger/National Geographic)

“Watch out!”

The scooter with the hyperactive exhaust system had narrowly missed grazing my husband. Only seconds before, I’d flattened myself against a stall to escape a passing moped. Our close encounters weren’t surprising; in places, the street we were walking on was little more than a couple of yards wide.

Details, Morocco

A stroll through the souks of Marrakesh’s old city, or medina, keeps you fully alert to what’s coming your way. The thoroughfares are so crazily crowded that you have to laugh. Every few seconds, you’re forced to get out of the way of a motorcycle or a bicycle; they stop-start through the throng as fast as they can, which can scarcely average faster than walking. Moments of paralysis occur when a hand-pushed cart carrying goods to a shop meets another carrying bricks or long pipes.

Medinas filled my childhood imagination. Even the word pleased me; it sounded so full of longing. I grew up mostly in raw new suburbs of the Midwest, in ranch- and colonial-style houses with large front yards. But my mother’s stories of Tripoli, which she’d visited before she got married, stirred my fancy. Instead of the mall, I wanted the ancient walled cities with their souks, fountains and mosques. Not to mention robed natives in interesting headgear and shopkeepers hawking handwoven carpets, exotic spices and delicately embroidered slippers.

Let’s face it: The lives of others often look more charming and interesting than our own.

Morocco’s medinas — the old, walled parts of the country’s cities — all have distinct characters, but a quality they all share is timelessness. We sampled several of these cities, as Edith Wharton had 95 years before, in 1917. According to her book “In Morocco,” the Moroccan city has no age, “since its seemingly immutable shape is forever crumbling and being renewed on the old lines.”

Rooftop vistas

Marrakesh’s medina revolves around the Jemaa el-Fna. A UNESCO World Heritage site, the square is a public space like no other and is less like a “square” than any other. Once used for public executions, it’s huge, and its shape can be described only as a spiky amoeba. Blocks of shops stick out into it; you go around corners, and new spaces open up.

We’d opted to stay close to the Jemaa el-Fna. Taxis can get into the square but no farther, and we would never have found our way to Riad Itrane if the owners hadn’t dispatched someone to lead us there. We walked a short way along one of the crowded streets, through an archway, and then turned into an alley, or derb. Here, the doors were wide apart; some were masterpieces of decoration and iron-studded strength.

The French owners, Patrice and Charlotte, greeted us with glasses of mint tea and briefed us on the city in Franglais. Outside our rooftop room, we could look up at the stars (which is what Itrane means in Berber) or down at the courtyards below, the vast roofscape — and a host of satellite dishes, which seemed to gaze up at the Atlas range to the south.

“Riad” means garden, but in Morocco it has come to mean a traditional city house with at least one interior gardenlike courtyard. Many have now been converted into guesthouses of a gentler and more colorful kind than hotels. Patrice and Charlotte had enlarged theirs by acquiring two other houses at the back and side. Thus it had become a complex of courtyards in various relations, connected by pillared spaces and laced with winding stairs and balconies and with extra apartments on the flat roofs — a maze, pleasantly decorated with Moorish mosaics and wrought iron and rugs and arches of the Moorish shape, like a round head on square shoulders.

We lolled on the roof terrace and made forays into the medina. We spotted some Westerners, but not as many as I’d expected. Mid-November is off-season, but the more likely explanation was the nail bomb that had gone off in one of the Jemaa el-Fna cafes in 2011, killing 17. The Arab Spring had scared tourists away from North Africa, although the Moroccan Kingdom is one of the few countries that escaped upheaval.

What there wasn’t a shortage of was cats. Morocco must have one of the world’s largest cat populations. (Is there such a thing as a cat census?) The ones we saw were on the thin side, but most looked healthy; their eyes were clear, their fur shiny. Apparently, cats aren’t privately owned in Morocco but more like community pets. They wander in the crowd, and people feed them scraps. Maybe cats prefer this free though precarious lifestyle. We saw few dogs, which Muslims regard as unclean.

Wharton wrote that Marrakesh “is the great market of the south, and the south means not only the Atlas with its feudal chiefs and their wild clansmen, but all that lies beyond of heat and savagery.”

We didn’t see camel caravans or wild men, but after dark the Jemaa el-Fna is lively with acrobats, musicians and pop-up restaurants serving tagine, a stewlike dish traditionally cooked in a special earthenware pot. At a long communal table, we ate an inexpensive, if not delectable, dish of overcooked vegetables on a bed of couscous. After dinner we were urged by other restaurateurs to sample their cooking. “You’re so skinny, you could eat two more meals,” was a standard line.

We were fascinated by the Gnawa musicians playing African Islamic spirituals. Their instruments were large metal castanets, three-string long-necked lutes and double-headed drums. If we strayed close, a member of the entourage would jump up and demand a donation.

Mazed and confused

After three delightful days, we made our way to Fes by train. The medina of Fes is huge, because it was once two cities, facing each other across a valley. They were founded in the 9th century and only united two centuries later. The combination is called Fes el-Bali, “old Fes,” because another medina — el-Jedid, “the new” — was built nearby in the 13th century.

Fes el-Bali is said to be the best preserved medieval town in the Arab world and another UNESCO World Heritage site. Most of its large, densely built-up area is free of cars.

Fes el-Bali is even more chaotic than Marrakesh, though in a different way. The souk’s steep, narrow, sunless passages are not for the claustrophobic, and you must be prepared to duck and weave if you want to break the logjam of people and donkey carts. The labyrinth of derbs and cul-de-sacs was fascinating and frustrating, and we spent our time drifting confusedly and getting lost.

We could have hired a guide, but that wouldn’t have been very adventurous, would it? If you ask people for directions, they’ll latch onto you and then expect money. Some use the ploy of taking you the long way around, thus justifying a larger tip. And if you do pay, they may brazenly tell you it’s not enough. “Ten dirhams is nothing!” one snapped at me. “Nothing!”

But then Moroccans seem to yell a lot. What you might think is a quarrel is just an ordinary disagreement.

Sadly, we became suspicious of any gesture of friendship. It’s hard to ignore a cheery “Hi,” but I took to scowling. “I have no more coins,” I said petulantly to one man.

“I don’t want your money,” he replied. “I just want you to be happy.”

At the heart of Fes el-Bali is the Kairaouine Mosque, one of the largest in Africa. As is true of nearly every other mosque in Morocco, non-Muslims cannot enter it, but you can glance in through open doorways.

I began to see the infrequent tiny squares such as the Place es-Seffarine as places of respite. Here we paused under the plane tree and listened to the metalsmiths beating their brass and copper cauldrons and kettles into shape.

The medina is home to small-scale capitalism, and any transaction generates a commission for a number of salesmen and touts. An urchin escorted us to a showroom in a dark alley where we half-heartedly haggled for a small kilim. We finally agreed on a price — probably paying too much.

Where we didn’t go were the tanneries. As Rough Guide publisher Mark Ellingham warns: “The tanneries, with their medieval vats filled with urine (and modern chemicals) for treating leather, are not for everyone.” Enough said.

We escaped the hustle by climbing several flights of stairs to a restaurant with a rooftop terrace. We’re vegetarians, and I was prepared for the fact that Morocco isn’t the easiest place to be one. Luckily, about half the menu at the Cafe Clock, just inside the beautiful blue-tiled gate called Bab Boujeloud, is meatless.

The thick harira soup served with a side of dates and shebbakiya (sesame cookies fried and then drenched in honey) was deeply satisfying after our hectic time below. I watched a large orange tabby cat cajole the diner at the next table into giving him part of his camel burger, the cafe’s signature dish.

Awash in blue

Marrakesh and Fes are just two of the four traditional capitals of Morocco, the others being Rabat and Meknes, and those and many other cities have their old medinas. On this trip we visited Tangier by the sea, Taroudant in the far south, and a place we hadn’t heard of — Chefchaouen.

Wharton didn’t visit Chefchaouen in the Rif mountains, because Christians were banned from it until the Spanish occupied it in 1920. But now you can get there by bus from Tangier or Tetouan. The bus leaves a valley to climb a mountainside and drops you in the newer part of the town, which climbs up to gates through the wall into the medina, which continues climbing; and on the skyline above is the double peak that gives Chefchaouen its name, which means “two goat-horns.”

Moorish refugees from Spain founded Chefchaouen in the 1470s, and Andalusian influences are everywhere. The medina, like others, is dense and throbbing with life and color — but especially the color blue. The plaster on the tumbled architecture isn’t just blue but intense blue; sometimes several shades of blue close together.

You can’t get seriously lost in this medina, because it’s only half a mile long. And you soon learn that it’s threaded by a main way that runs along the upper wall, from which smaller openings twist down to the tree-shaded public space along the lower wall above the river. This has to be one of the most pleasant of all “squares” (and it is nearly rectangular, very unlike Marrakesh’s Jemaa el-Fna). One side is bounded by the mosque, to which the congregation climbs up a long staircase. On the other sides are open-air restaurants, where you may be entertained at your table by a musician with tribal garb and a native fiddle. Chefchaouen’s people cross paths in this square, and while we were there, a circle gathered and chanted slogans called out by a leader. Then they marched off carrying signs about Palestine.

The riad in which we stayed was another little domestic maze. Our room on the roof had a wooden ceiling worthy of a church. Outside it, we had breakfast on a blue-washed balcony, from which we looked down across the jumble of the medina as the sun began to climb over the two-horned mountain and touch it.

Lavenas is a freelance writer based in Lyme Regis, England.