Chaos surrounded us. Informal porters rolling luggage carts zigzagged between cars. Commuters spilled from the bus terminal onto the sidewalk, where they sat on suitcases and duffel bags. Minibus taxis zoomed through the congestion, pedestrians be damned.

Our car crawled past a barbed-wire fence and reached a sliding gate, where all that separated my wife and me from the empty lot on the other side was a security guard. “Blue Train,” I said, and the guard waved us through.

We pulled up to a blue carpet next to the central train station in Cape Town, South Africa, where two butlers in blue vests and white gloves greeted us by name and unloaded our luggage before ushering us into a waiting room that was decidedly more upscale than the one in the adjacent building for bus travelers. We lounged on plush sofas with a few dozen other passengers, relaxing to piano music and enjoying a spread of fruit, pastries, sandwiches and sparkling wine.

I had traveled to this stunning South African metropolis in December to indulge in a bit of luxury by taking a trip on the country’s legendary Blue Train. It’s an adventure that turns a two-hour flight (plus a 45-minute drive) from Cape Town to Pretoria into a two-night, 994-mile experience.

The Blue Train, whose origins date back a century, is meant to evoke exclusivity. And that’s exactly what you feel when you’re waiting in the lounge, a separate world from the gritty urban commotion outside.

The whiplash between excess and destitution is something that I still haven’t grown comfortable with in my nearly four years as the Johannesburg bureau chief for The New York Times.

South Africa is the most unequal country in the world, according to the World Bank, a reality on display almost anywhere you travel across this vast nation of more than 60 million people.

The inequality is in many ways the legacy of apartheid, a decades-long racial caste system in which the country’s former white-minority government violently segregated nonwhite South Africans into communities that were left to rot.

During the thick of apartheid just more than 40 years ago, Joseph Lelyveld, one of my predecessors in this job and a former executive editor at the Times, chronicled this same trip on the Blue Train in an article for the Travel section. Although it was the country’s only desegregated train at the time, it was rare to see Black passengers because of the high cost. The train, he wrote, offered “a window on a society that is compartmentalized on or off the rails.”

Lelyveld, who died last year, added, “Outside the train, you can catch some reasonably telling glimpses of the uneven ways in which land and labor are apportioned under the South African system.”

Now, three decades after that racist system had been undone, I wondered how the trip and the country it showcases might have changed. Racial segregation is no longer legal, and South Africans may now live and work wherever they want, regardless of skin color.

But from the moment I walked into the lounge, I realized that this trip would offer a firsthand view of the staggering inequality and racial divide that continue to bedevil South Africa.

The cost of the train remains prohibitive for many. Rates, which include seven meals and as many drinks as you want over a roughly 54-hour trip, start at nearly $4,000 for two people for the lowest room type in the low season, and climb to more than $6,000 for the most luxurious rooms during high season.

My wife and I accounted for two of the four Black passengers. The dozens of other passengers were white — mostly foreigners, but also a handful of South Africans. It was the other way around for the staff: All but one of them were either Black or colored, a multiracial classification in South Africa.

Our departure time came and went while we waited. The train manager offered an explanation: Someone had pelted the train with stones as it approached the station that morning, and technicians were repairing a shattered window.

While the pelting could have been the result of youthful mischief, I couldn’t help but think about something many South Africans have told me: The ostentatious display of excess by the privileged few can feel like a slap in the face to the masses who have been excluded from post-apartheid prosperity.

We took off around 2 p.m. on a Thursday, with our arrival in Pretoria scheduled for Saturday afternoon. Because we were leaving late, there was no formal lunch; instead, the staff set up light bites in the observation car.

I loaded up a plate and settled in as the train crept out of central Cape Town. The first part of the trip cut through several townships, the outlying communities that the government restricted nonwhite South Africans to during apartheid and that today still largely suffer the effects of poor investment.

There were piles of trash on either side of the track in some places. Cube-shaped tin homes hugged the tracks so closely that it seemed as if the corrugated roofs of some might scrape the train. A private security car drove alongside the tracks; a staff member later told me that security escorted the train through certain urban areas to thwart potential vandals. Some locals stared as the train jogged past, while giddy children waved and smiled.

There are complicated reasons why parts of South Africa are struggling. But no one seemed in the mood to delve into the prickly nuances of mass dispossession, inherited wealth disparities, poor governance or the influence of global finance. We were just under three hours into the trip, and the scenery became so captivating that pretty much everyone in the car fell into a silent awe.

The train, perched above the Little Berg River, snaked its way through a valley north of the wine town of Paarl, with rocky green and yellow hillsides around it. We chugged on through Western Cape province landscapes so consistently majestic that you could easily begin to take them for granted.

The train stopped for a water refill at the Worcester station. That’s where we were served dinner by the staff. Dinner was meant to be an elegant affair, with men asked to wear blazers and women evening dresses.

The stop was scheduled for about 30 minutes, but the train stayed for more than an hour and a half. No explanation was given. Some South Africans had warned me to be prepared for unexpected delays. The country’s rail infrastructure has been beleaguered for years, in part because of corruption and mismanagement within Transnet, the state-owned rail company that owns and operates the Blue Train.

In the morning, the lush greenery of the Western Cape had given way to the brown shrubs of the semiarid Northern Cape province. The scenery was different but no less spectacular. This was the Blue Train at its finest, showcasing South Africa’s vast and diverse beauty.