From his perch on a rooftop terrace near the crenelated western ramparts of the walled city, a visitor from England watched the sun set in spectacular display over the Atlantic Ocean. As it disappeared on the cloudless horizon, the sun's rays cast a golden glow on a rising crescent moon decorated on the eve of the summer solstice by a silvery alignment of planets and stars. “Nice touch, that,” quipped Bill Corbett, a 39-year-old London photographer, D.J. and music fanatic whose visit to Essaouira, an exotic, wind- and sun-swept Moroccan city on the northwest coast of Africa, for the 10th-annual Gnawa and World Music Festival, would prove a transforming experience. “This really is a midsummer night's dream.” And one with an exhilarating soundtrack — courtesy of 25 Moroccan Gnawa musical brotherhoods, whose exuberant, hypnotically rhythmic and joy-infused music drew an estimated 400,000 fans from Morocco and across North Africa, Europe and North America to Essaouira last June for the five-day festival. Midsummer revelers heard more than 30 other jazz fusion, rock, reggae, African, Brazilian, Afro-Cuban and hip-hop acts from more than a dozen countries — as well as Hoba Hoba Spirit, a crowd-wowing multilingual “Moroc 'n roll!” band from Casablanca — performing on nine festival stages scattered in and around Essaouira's walled, maze-like medina. Unlike festivals staged, à la Woodstock, in muddy, middle-of-nowhere pastures, or worse, in vast, overheated football stadiums, the Gnawa's setting, in a small, friendly and almost impossibly picturesque, wind-cooled seaside city, is as magical and mesmerizing as the music. “I thought about going to Glastonbury,” Mr. Corbett said of the popular rock festival outside London that was being muddied by cold, drenching rains as he spoke. “Why see a bunch of boring new rock bands when you can see amazing ancient ones?” he said. He made a sweeping gesture that took in the stars, the moon, the walls of the ramparts silhouetted against the sky and the gleaming superstructure of the main festival stage below on the Place Moulay Hassan, Essaouira's central square. “And in a setting like this?” We were both still buzzing after catching an 11 p.m. concert by a Gnawa group led by the maalem (master musician) Abdenbi El Gadari, on a small festival stage in the Marché aux Grains (Grain Market), a square enclosed by colonnaded arches where, until little more than 100 years ago, slaves were bought and sold at auction. Mr. Gadari sat on a floor cushion at the center of a stage covered with lush, colorfully patterned Moroccan carpets. Arrayed around him were more than a dozen members of his musical brotherhood: drummers, steel castanet players, dancers and singers who wore ankle-length white satin robes and tasseled fezzes beaded with white seashells. With the commanding presence of the American Delta bluesmen with whom he shares musical roots and mojo, the maalem thrummed and plucked bluesy figures on his guimbri — a three-string lute, much like a bass guitar — and sang a gospely call and response in a rich baritone. The music built slowly to an exhilarating crescendo of intricate rhythms and cross rhythms created by propulsive beating on hand-held drums and large, tambourine-like bendirs, staccato hand claps and the incessant, syncopated clack and clatter of the steel castanets. As the spirit moved them, musicians put down their instruments and stepped to the front of the stage to dance, displaying footwork and moves that resembled those of their African-American cousins, from the Temptations' stylized line steps and James Brown's knee drops to hip-hop's exaggerated lopping turns, as well as whirls, leaps, back flips and hussar-like two-legged kicks that defied choreographic categorization, not to mention gravity. Like all Gnawa brotherhoods, Mr. Gadari's group performed music that for centuries was played only in secret spirit-possession and healing ceremonies called lilas that have evolved from ancient African animistic and Sufi rituals. The brotherhoods continue to perform in such religious rites — though only in strictly private gatherings — in which conjured healing spirits are said “to mount” the possessed, who whirl and writhe in ecstatic trance, during which they often cut or flail themselves with ceremonial daggers or iron batons. “To me, these spectacles are filled with great beauty,” Paul Bowles, the expatriate American composer and writer who spent much of his life in Morocco, wrote of often bloody ceremonies that most Westerners would find gruesome to behold, “because their obvious purpose is to prove the power of the spirit over the flesh.” “YOU'RE American?” the unsmiling young Moroccan manning a closet-size shop in the Spices Souk demanded. I balked, wondering if it would be wise to say I was Canadian, as advised in a post on a travel blog by a countryman concerned, as I was, about anti-American sentiment stirred by the war in Iraq, 3,000 miles east. After the bombings in Casablanca in 2003 and 2007, the State Department's warning in its information about traveling to Morocco that “the potential for terrorist violence against American interests and citizens remains high” gave me pause. But my anxieties evaporated as soon as I arrived in Marrakech, where the cab ride from the airport to my hotel in the center of the red-hued medieval city was a senses-jolting experience. Minicabs and motorbikes sped through ancient streets filled with horse-drawn carts, donkeys laden with bulging sacks and pedestrians dressed as though they'd been plucked from the streets of Jerusalem at the time of Jesus. The next day, after a two-and-a-half-hour cab drive west through forbidding desert landscapes where funnel clouds of brown dust rose in the distance and a goatherd sought refuge from the blistering sun in the nearly nonexistent shade of a lone and scrawny tree, I arrived in Essaouira. Its sprawl of white homes and apartment buildings was spread along low hills overlooking a broad, blue bay and the tall, sand-colored walls that enclosed the medina, the heart of the old city. “You will love Essaouira,” a young Moroccan woman who is studying architecture in Florida assured me before I left. “It's magical.” I understood what she meant after a single afternoon and evening walking around the medina, with its dramatic, castlelike battlements where ramparts bristle with cannons pointing out to sea, its dark, serpentine alleyways, hidden courtyards, graceful archways and sunny, colonnaded squares and its souks selling spices, oils, aphrodisiacs and the makings of potions said to cast spells. At first glance a bewildering maze of circuitous, tunnel-like side streets, the medina, commissioned by an 18th-century sultan and designed, by a French architect, Theodore Cornut, with two wide boulevards as central axes and thoroughfares linking three main gates, soon proved easily navigable. From the humanlike cries of gulls reeling in the cloudless skies and the cooling Atlantic trade winds whistling through the streets and rustling the crowns of the ancient palms, to the staccato chatter of street musicians' steel castanets and the muezzin's musical, mystical call to evening prayers, the city was alive with sound even when the festival stages stood silent. After an hour of aimless wandering, I found myself in the Spices Souk, where the young merchant demanded to know if I was an American. “Yes,” I finally replied. “I'm from New York.” “You came all this way to visit my country?” the man exclaimed. With that, he poured heaping tablespoons of powdered saffron into a plastic bag and handed it to me. “I was 19 and a hippie among thousands of them who came to Morocco in those days,” Loy Ehrlich, a French guitarist who is an artistic director of the Gnawa Festival, recalled of his first pilgrimage to Essaouira in 1971. Then an out-of-the-way, off-the-tourist-map town with no rail or regular bus service, no good roads to accommodate it if there were and only a handful of hotels, Essaouira, formerly known by its Portuguese name, Mogador, was beloved by Moroccans for its beauty, its near-perfect climate — even in the depths of the African summer when the interior swelters, ocean breezes sweep clouds from the sky and keep the so-called Windy City pleasant in the day and cool at night — and its friendly inhabitants. (“Essaouira people always smile,” said a Gnawa fan from Fez). Artists like Bowles, who visited in 1959 while recording Gnawa music for the Library of Congress, and Orson Welles, who spent nearly two years there off and on in the late 1940s and early 50s filming “Othello,” using the city's ramparts, hammams and arched gateways as a fitting North African backdrop for the tragedy of the Moor, were also charmed by the city's beauty. But it was a sojourn by Jimi Hendrix, the guitar idol who vacationed in Essaouira in 1969, a year before his death, that inspired visits by Mr. Ehrlich and countless other musicians and fans, many of whom believe that Hendrix's song “Castles Made of Sand” was inspired by Borj El Baroud, the ghostly, turreted remains of a Portuguese fort decomposing off the beach south of the city. Like most of the legends surrounding Hendrix's brief visit, when he supposedly spent weeks jamming with local musicians, the story is a pipe dream. Hendrix, who arrived without a guitar and spent a single night or two in the Hôtel d'Iles, recorded the song two years before he arrived in Morocco. It was Hendrix's star that Mr. Ehrlich followed to Essaouira. But it was Gnawa music that transfixed him when he arrived. Terminus of the ancient desert trade routes to Timbuktu and sub-Saharan Africa, where the Gnawas' enslaved ancestors had their origins, Essaouira remains home to the largest concentration of the musical brotherhoods in Morocco. “When I first heard Gnawa, it was like a discovery, like something was revealed to me,” Mr. Ehrlich said. “I felt the power of the music and its connection to the blues. The Africans who were brought to America created the blues; those sent to Morocco created Gnawa. It was like two worlds mixing — the African and rock 'n' roll.” Performing on the third night of the festival with a group formed for the occasion in homage to Hendrix, Mr. Ehrlich led the Band of Gnawa (after Hendrix's Band of Gypsys), a cross-cultural mix of musicians playing electric guitars, guimbri, bendirs, electric keyboards and castanets, through renditions of Led Zeppelin's “Immigrant Song” (with a maalem in emerald satin channeling Jimmy Page on his guimbri), as well as Hendrix's “Stone Free” and, yes, “Castles Made of Sand.” “In a crazy world where rich and poor, black and white are divided,” Mr. Ehrlich said, “musicians try to create something to unite the world.” In keeping with that creed, he rewrote the refrain of a familiar Beatles singalong. “Come together, right now,” the vast crowd gathered on Place Moulay Hassan sang along with Mr. Ehrlich's Band of Gnawa, “Essa-Weer-Ah!” - NYT __