The latest hit for “Saturday Night Live” on the Internet began taking shape on Sept. 24, when Andy Samberg and a group of his colleagues on the show were sitting around their offices, scrounging for a celebrity or politician “who could really use a love song right now.” Around the same time, Seth Meyers, the show’s head writer, was imagining a skit in which Mahmoud Ahmadinejad, the president of Iran, would be depicted on a wild night out on the town.

Before long the ideas fused, and by last Saturday, opening night for the 33rd season of “Saturday Night Live,” the group had produced a three-minute music video titled “I Ran So Far Away.” In it, a crooning, piano-playing Mr. Samberg and a doppelgänger for the Iranian president (his castmate Fred Armisen, bearded and in a gray suit and open-neck dress shirt) dance and appear to fall in love — giving apparent lie to Mr. Ahmadinejad’s contention at Columbia University that there were no homosexuals in Iran.

By yesterday afternoon, just four days after its network premiere, the various versions of the video posted on YouTube had been viewed nearly 300,000 times, according to tallies posted on the site. For Mr. Samberg and his main collaborators — Akiva Schaffer and Jorma Taccone — “I Ran” is something of a sequel. Last year, NBC posted an uncensored version of a boy-band video the group made — the cleaned-up title was “Special Treat in a Box” — in which Justin Timberlake and Mr. Samberg each appeared to be making a gift of their male anatomy. Thus far, that film has been seen more than 29 million times on YouTube, and last month it won an Emmy.

However popular, such films — including “Lazy Sunday,” a rapping homage to “The Chronicles of Narnia” — have not appeared to raise the ratings of the show itself. Last season, “Saturday Night Live” drew an average of about 6.4 million viewers a week, which was down from the year before. But the digital shorts have given the show, and Mr. Samberg in particular, a Web presence and cachet.